Array comprehension syntax
Tab Atkins Jr.
jackalmage at gmail.com
Fri Sep 21 18:40:33 PDT 2012
On Fri, Sep 21, 2012 at 4:32 PM, Jason Orendorff
<jason.orendorff at gmail.com> wrote:
> 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.
I assume this is meant to match Python's syntax.
> 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.
No strong opinion, but I'd be fine with your suggested change.
Sequencing commas in an if statement are weird anyway.
> 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.
Again, no strong opinion, but this kind of thing is occasionally
useful, despite the hazard.
> 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.
I assume that they're always distinguishable due to the
presence/absence of a space after "each", yes.
More information about the es-discuss