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

Brendan Eich brendan at mozilla.com
Sun May 15 10:09:54 PDT 2011


On May 15, 2011, at 8:14 AM, Dmitry A. Soshnikov wrote:
> // Use # to freeze and join to nearest relevant closure
> function return_pure() {
> 
>   
> return #(a)
>  -> a * a;
> 
> }
> 
>  
> let p = return_pure
> ()
> ,
>     q = return_pure
> ()
> ;
> assert
> (p === q);
> So, ES3 joined-objects are back. Though, the question is in their [[Scope]] difference (i.e. can be they joined in the following case):

The "join to nearest relevant closure" words were meant to address this. Let's see (I made a few typo and syntax fixes):


> let foo = (...args) -> {
>   return #(a) -> args.map(a);
> };
> 
> foo(1, 2, 3)((x) -> x * x); // [1, 4, 9]
> 
> foo(5, 10)((x) -> x * x); // [25, 100]
> 
> Do both have the same (reflected) [[Scope]] to be joined?

The two foo calls have different unjoinable scope chains enclosing variables captured by the returned #(a) -> args.map(a) function, specifically args. So that hash-frozen arrow-function cannot be joined into one identity.


> At first glance with named args, e.g. foo(a, b, c) yes, but what's with rest args?

Named vs. rest doesn't matter, the question is lexical capture. The name |args| is from the outer

(...args) -> { return #(a) -> args.map(a); }

function. It is used in the inner arrow function.

The location of this |args| parameter is what is actually captured in general. This is optimizable to copying the value at that location in many cases, including this one, but that doesn't change the fact that the location varies with each call to foo.

So args' abstract location differs, and therefore its value may differ, each time the inner arrow-function is evaluated. So the # freezes it but it will not be joined with other evaluations of the same source form.

However you implement closures, something has to vary each time the inner arrow is evaluated, to capture the correct args, [1, 2, 3] or [5, 10].

/be

> 
> Other things seems fine.
> 
> Dmitry.
> 
>> /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 )
>> 
>> 
>> _______________________________________________
>> es-discuss mailing list
>> 
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list