Expression closures - use-cases for shortcut lambda syntax(blocks)

P T Withington ptw at pobox.com
Wed Mar 21 13:38:23 PDT 2007


On 2007-03-21, at 13:44 EDT, Vassily Gavrilyak wrote:
> ---------- Forwarded message ----------
> From: Vassily Gavrilyak <gavrilyak at gmail.com>
[...]
>
> There are also versions with try-finally, even more painful.
> The same about every other use-cases.

Another solution would be to add macros to the language, so that the  
try/finally version was cleaner.  That's the lispy way:

macro withPreparedStatement {
   withPreparedStatement (??prepared:variable = ?statement:expression  
on ?connection:expression) ?body:statement =>
    { let ?prepared = ?connnection.prepare(?statement);
      try {
        ?body
      } finally {
        ?connnection.deallocate(?prepared);
      }
    }
}

function doSomethingWithNewPeople()
{
   withPreparedStatement(selectStatement =
       "SELECT code, name from people where status='new'" on  
connection) {
     withPreparedStatement(insertStatement  =
         "INSERT INTO young(name,age) values (?,?)" on connection){
       connection.transaction(function(tx) {
         for (let [name, age] in selectStatement.executeQuery()) {
           if (name.match(/.*Joe/) && age % 2)
             insertStatement.execute(name, age);
         }
       });
     }
   }
}

:)

[Yes I know the macro idea has been deferred.]




More information about the Es4-discuss mailing list