Expression Closures as Compliment to Arrow Functions

Brendan Eich brendan at mozilla.org
Tue Mar 24 13:26:28 UTC 2015


Bergi wrote:
> Jacob Parker schrieb:
>> Either I'm wrong, or that's missing some parens. Assume the following 
>> (the
>> only interpretation I can see to not throw a syntax error),
>>
>>      var y = (function (a) a ? f : x++)(1);
>>
>> In which case, that could only return `f`, which when printed should 
>> print
>> the source of f, no?
>
> Uh, I had some problems with that either, but finally got it now. If 
> we make the parens explicit, the point is that the expression should 
> be evaluated as
>
>   var y = ((function (a) a) ? f : x++)(1); // === f(1)
>
> instead of
>
>   var y = (function (a) (a ? f : x++))(1); // === f

That's right.

> Admittedly, I think arrow functions have same problem. How is
>
>   var y = (a) => a ? f : x++ (1);
>
> evaluated (or is it syntactically valid at all)?

The problem with expression closures is precedence inversion: you have 
LowPrec ~~> HighPrec ~~> stuff ending with LowPrec. The particular 
nonterminals are AssignmentExpression and PrimaryExpression.

No such problem afflicts arrows, because they are low-precedence, in 
fact AssignmentExpression alternative right-hand sides. So your example 
here is a syntax error, because you are placing two assignment 
expressions next to one another with no operator or semicolon in between.

/be


More information about the es-discuss mailing list