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

Yuh-Ruey Chen maian330 at gmail.com
Sat Mar 17 00:45:55 PDT 2007


Jeff Walden wrote:
> Neil Mix wrote:
> > addLoadEvent() {
> >      var elems = getElementsByTagAndClassName("A", "view-source");
> >      var page = "rounded_corners/";
> >      for (var i = 0; i < elems.length; i++) {
> >          var elem = elems[i];
> >          var href = elem.href.split(/\//).pop();
> >          elem.target = "_blank";
> >          elem.href = "../view-source/view-source.html#" + page + href;
> >      }
> > }
>
> This syntax is whitespace-significant; the presence or absence of a line terminator between the closing parenthesis after the function name and the opening brace affects behavior.  If a line terminator occurs, semicolon insertion transforms it into a method call of no arguments and a block statement.  If a line terminator doesn't occur, your behavior works (in an ES4 implementation, that is).  The existing no-line-terminator uses are annoying enough, particularly in the case when I want to return an object from a method, and I'd rather see as few new instances (which don't mimic the existing ones) as possible, personally.
>
> Jeff
>
>   

Some musings...

We can avoid the auto-semicolon problem be requiring some sort of token
right after the function call, e.g. "do" or "=>" or ":". Then the
brackets can go on the next line.

foo() do
{ ... }

foo() =>   // this isn't intuitive at all
{ ... }

However, only one lambda can be used with this syntax. To use more
lambdas, we'd have to go back to the normal syntax for all lambdas
except the last:

bar(function(...) ...) do {
   ...
}

which IMO is a bit inelegant. Or maybe we could chain the blocks:

bar() do
{
} do   // notice that this has to be on the same line as the closing "}"
of the last block to avoid semicolon insertion
{
}

which still sucks and only works if there are no non-function arguments
between the function arguments (e.g. how you would you convert
bar(function() 1, "hi", function() 4) to the above chained block form?).

This goes for any other solution that resorts to putting the lambda body
after the ")" in function call. While it does get rid of the closing ")"
after the function arguments, it brings up way too many other problem IMO.

-Yuh-Ruey Chen



More information about the Es4-discuss mailing list