Array comprehension syntax

Allen Wirfs-Brock allen at
Fri Sep 21 19:18:42 PDT 2012

On Sep 21, 2012, at 4:32 PM, Jason Orendorff wrote:

> ...
> 2.  These comprehensions are permitted:
>        [EXPR for x of obj if a, b, c]
>        [EXPR for x of obj if x = 3]
>    The first is surprising to me for two reasons: first, because
>    commas in an array literal usually separate array elements,
>    whereas these are sequencing commas; second, it's odd that
>    commas are not permitted in the EXPR part of the comprehension
>    but are permitted in the if-condition.

Allowing comma in the EXPR would permit things like:

 [ thisIsALongSubExpression,
   x for for x of obj

This would at least be confusing to human readers and recursive decent parsers would probably prefer to not have to deal with it.

The notes in the draft also suggest the possibility of using AssignmentExpressions in the for and if parts.  I think that would probably be a good thing to do.   

>    The second is not so surprising, but it occurs to me that
>    unparenthesized assignment in this context will usually be a
>    mistake, so it might be nice to make that a syntax error.

yes but a parenthesized assignment in that position is also likely to be a mistake and there isn't any good syntactic way to prevent that.

I could probably go along with either AssignmentExpression or ConditionalExpression here.  But, I'm a bit comfortable about nanny syntax restriction because they add complexity and makes the language  seem arbitrarily inconsistent. 


More information about the es-discuss mailing list