return when desugaring to closures
Waldemar Horwat
waldemar at google.com
Thu Oct 16 11:38:55 PDT 2008
Brendan Eich wrote:
> On Oct 15, 2008, at 2:36 PM, Waldemar Horwat wrote:
>
>> There is no such thing as a "let expression".
>
> Let expressions
> <http://developer.mozilla.org/en/New_in_JavaScript_1.7#let_expressions> in
> JS1.7 (Firefox 2+), based on the ES4 proposal
> <http://wiki.ecmascript.org/doku.php?id=proposals:block_expressions>.
> ES3-ish grammar:
>
> /LetExpression /*:*
> * let*/ /*( VariableDeclarationList/ /*) [lookahead ∉
> {*{*}] AssignmentExpression**
>
> produced from PrimaryExpression.
That's not a valid grammar. You can't have an AssignmentExpression terminating a PrimaryExpression. It leads to trouble such as:
let a = b + c
being interpreted as both:
(let a = b) + c
and:
let a = (b + c)
>> It's mandatory because the grammar and semicolon insertion rules say
>> so. Btw, to properly terminate a lambda expression you'd need *two*
>> semicolons. Here's why one would be insufficient:
>>
>> f = lambda(x) x;
>> (a + b) && c;
>>
>> would parse the body of the lambda as the expression "x;", the "(a +
>> b)" as an argument to the lambda, and the rest as applying to the
>> result of calling the lambda. What you'd want to write instead would be:
>>
>> f = lambda(x) x;;
>
> Expression closures
> <http://developer.mozilla.org/en/New_in_JavaScript_1.8#Expression_closures> in
> JS1.8 (Firefox 3+) do not have this problem. Using ES3's notation:
>
> /FunctionDeclaration /*:*
> * function */Identifier /*( */FormalParameterList//opt /*)
> */FunctionBody/
> /FunctionExpression /*:*
> * function */Identifier//opt /*( */FormalParameterList//opt /*)
> */FunctionBody/
> /FormalParameterList /*:*
> / Identifier/
> / FormalParameterList /*, */Identifier/
> /FunctionBody /*:*
> / { *SourceElements */*}*
> * */[lookahead ∉ {*{*}] AssignmentExpression/
That's not a valid grammar. For example,
a = function(x) b+c
can parse as, among other things:
a = (function(x) b) + c
which is probably not what you want.
Waldemar
More information about the Es-discuss
mailing list