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

Brendan Eich brendan at mozilla.org
Mon Mar 19 15:36:48 PDT 2007


On Mar 19, 2007, at 2:55 PM, Jon Zeppieri wrote:

> On 3/19/07, P T Withington <ptw at pobox.com> wrote:
>> On 2007-03-19, at 17:35 EDT, Jeff Dyer wrote:
>>
>>>      let (x=y) x
>>>
>>> become
>>>
>>>      let (x=y) => x
>>
>> I thought it became:
>>
>> (x) => x (y)
>>
>
> ...which brings up the issue of associativity.  When I first looked at
> your example, I thought x was being applied to y, rather than (x) => x
> being applied to y.

Forgot to answer the associativity question. With ES3 function  
expressions, which are primary expressions, you need not parenthesize  
the function expression to call it. Just append (actuals) to invoke:

js> 1 + function(){return 2}()
3

Note the 1 + or some other left-expression-context is necessary.  
Oherwise, with just:

js> function(){return 2}
function () {
     return 2;
}

you have written a useless unnamed function expression statement that  
ends at the } that closes the body, and any () after will provoke a  
syntax error:

js> function(){return 2}()
typein:4: SyntaxError: syntax error:
typein:4: function(){return 2}()
typein:4: .....................^

This is due to

12.4 Expression Statement
Syntax
ExpressionStatement :
[lookahead ∉ {{,function}] Expression ;

in ECMA-262 Edition 3.

Without the "clutter" => connecting the (formals) and assign-expr  
body, we are free to match the ES3 behavior and not require  
parentheses around the function expression in order to apply it.

So => could be considered an operator with high precedence, but then  
almost all expression bodies would have to be parenthesized. If it is  
low precedence, then you will need to parenthesize ((x) => x)(y). I  
think Yuh-Ruey Chen already proposed that => be low precedence, but  
higher than the comma operator, in an earlier post.

This seems to me another advantage of => -- that it facilitates  
operator precedence parsing of expression closures.

/be
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.mozilla.org/pipermail/es-discuss/attachments/20070319/7b7381f5/attachment-0002.html 


More information about the Es4-discuss mailing list