Array comprehension syntax

Jason Orendorff jason.orendorff at gmail.com
Fri Sep 21 16:32:40 PDT 2012


I have a few proposals and questions about ES6 array comprehensions:
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-11.1.4.2

1.  It seems simpler and more general to accept arbitrary sequences
    of 'for' and 'if' clauses:

        [x if x !== undefined]
        [g for u of users if u.isAdmin() for g of u.groups()]

    In the current draft only [Expr ForClause+ IfClause?] is allowed.

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.

    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.

    So I propose changing "if Expression" to
    "if ConditionalExpression" in the ArrayComprehension
    construction.

3.  SpiderMonkey already supports this nonstandard syntax:
        [x for each (x in obj)]
    A paren-free ES6 array comprehension could begin with a function
    call, like this:
        [x for each(x in obj).y of z]
    Currently SpiderMonkey treats 'each' as a keyword when it
    appears after the 'for' keyword.

    The two syntaxes are distinguishable in all cases, right?
    It's a little painful to get NotIn right in this case, but we can
    hack it.  As long as the syntax is unambiguous.

-j


More information about the es-discuss mailing list