arrow syntax unnecessary and the idea that "function" is too long

Brendan Eich brendan at mozilla.com
Sat May 14 23:33:47 PDT 2011


On May 13, 2011, at 11:25 PM, Brendan Eich wrote:

> I'll update http://wiki.ecmascript.org/doku.php?id=strawman:arrow_function_syntax to include an ES5 grammar patch.

Done: http://wiki.ecmascript.org/doku.php?id=strawman:arrow_function_syntax, specifically http://wiki.ecmascript.org/doku.php?id=strawman:arrow_function_syntax#grammar_changes

This requires splitting the sub-grammar starting at Expression (comma expression), at AssignmentExpression (which occurs in all comma-separated list non-comma-expression contexts, namely argument lists and initialisers), creating a parallel production chain from Statement through AssignmentStatement.

Many languages restrict assignment to statement form. We don't need to do that and shouldn't break extant code that nests assignment expressions, but separating assignment statements (comma-separated lists of assignments) enables better right-hand side syntax for arrow function expressions -- namely, they don't have to be parenthesized.

Thus you can write

array.map((elem) -> elem * elem)
let identity = (x) -> x;
thrice = (x) -> 3 * x;

without gratuitous parenthesization. Calling an arrow function expression requires parentheses on the outside, of course:

alert((-> "paren me")());

Comments welcome, I haven't had time to formalize and test this but it looks like it will work.

/be

Grammar Changes

Change all uses of AssignmentExpression outside of the Expression sub-grammar to InitialExpression:

ElementList :        // See 11.1.4
    Elisionopt InitialExpression
    ElementList , Elisionopt InitialExpression
 ...
PropertyAssignment : // See 11.1.5
    PropertyName : InitialExpression
 ...
ArgumentList :       // See 11.2
    InitialExpression
    ArgumentList , InitialExpression
 ...
Initialiser :        // See 12.2
    = InitialExpression

InitialiserNoIn :    // See 12.2
    = InitialExpressionNoIn
Define InitialExpression and ArrowFunctionExpression:

InitialExpression :
    AssignmentExpression
    ArrowFunctionExpression

ArrowFunctionExpression :
    FormalParametersOpt Arrow [lookahead ∉ {{}] InitialExpression
    FormalParametersOpt Arrow Block

FormalParameters :
    ( FormalParameterListOpt )
    
Arrow : one of -> or =>

Split assignment out of ExpressionStatement into AssignmentStatement:

Statement :
    Block
    VariableStatement
    EmptyStatement
    AssignmentStatement
    ExpressionStatement
    ...

AssignmentStatement :
    [lookahead ∉ {{, function}] AssignmentList ;

AssignmentList :
    Assignment
    AssignmentList , Assignment

Assignment :
    LeftHandSideExpression = InitialExpression
    LeftHandSideExpression AssignmentOperator InitialExpression

ExpressionStatement :
    [lookahead ∉ {{, function}] ConditionalExpression ;
Finally, PrimaryExpression produces a parenthesized ArrowFunctionExpression:

PrimaryExpression :
    ...
    ( ArrowFunctionExpression )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110514/8698179f/attachment-0001.html>


More information about the es-discuss mailing list