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

Vassily Gavrilyak gavrilyak at gmail.com
Thu Mar 15 17:37:58 PDT 2007


I like the expression closures were accepted into the spec, they will
nicely cover many use-cases for iterating, sorting, etc. using closures.
And I was the one who suggested Brendan on his blog to bring the issue of
shortcut function syntax.
Actually I proposed a more simpler solution, just using lambda-like syntax
from Haskell.

However I didn't provide more use-cases where it can be useful and allows to
not add support for
such use-cases with some special language construct.

In short - look how Ruby uses blocks.

So, just a few from the top

1. Automatic resource deallocation.
using(File.open("test"), function(file){
   //do something with file here..
}); //auto-close of file here.

2.Transactions (mixed with resource usage) for frameworks like Phobos
(Phobos on Rails? :-) )
connection.transaction( function( tx){
    using(connection.prepare("INSERT INTO ..."), function( statement){
        using(statement, function(recordset){
             processRecordset(recordset);
        }) // recordset closed
    }); //statement closed
})//commit-rollback depended on whether exception was thrown

3. CPS-style asynchronous processing
3.1 - AJAX
server.getEMails(function(emails){
   eMails.forEach(function(email){
      if(somethingInEmail(email)){
        server.forwardEMail(function(response){
              processForwarding(responce);
        })
      }
   })
})
3.2 animations (Flash or DHTM)
Something like - move ball, when it hits the wall, explode can be clearly
expressed with cps-style too.

4. Some simplest idioms like 10.times do {} from Ruby.
(10).times(function(){
   print("Hi");
})

Now, all those "function" just doesn't look like functions, bat rather
blocks.
So word function is meaningless here and unnecessary complicates the code.


Maybe the best solution here is too introduce some special syntax for
resource allocation and async programming.
That what some languages do and ES is still not here. But I doubt that
transaction support will ever be in language.
And there probably many more usages for blocks.

In our team we currently using modified SpiderMonkey for JS and  MTASC for
AS  with  modified
lexer (not parser) for support shortcut syntax. We use this syntax quite
heavily and found it  very useful and not a false economy.
Modification is VERY simple (2 lines in both cases) - when lexer found "(\"
replace it with "function("

This way ES could almost for free have a feature that many languages (C# 3,
Ruby, Smalltalk, Perl) already have
and developers found this feature useful. Java seems going there too with
closures proposal.

And yes, this is just syntax sugar and nothing new for ES that aren't
already present.
But developers will fail to recognize that ES actually have blocks just
because of syntax.
I am the example of such developer :-).

Hope it's probably not to late to address those issues.

Regards,
Vassily Gavrilyak
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20070316/7d535725/attachment.html 


More information about the Es4-discuss mailing list