Array comprehension syntax

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Sep 22 09:11:03 PDT 2012


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

> 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.

Allowing an arbitrary sequence of |for| and |if| clauses is arguably more complex and harder to understand than the current proposal for a sequence of |for| clauses followed by a single optional |if| clause.   

My personal bias, is that comprehensions are just sugar that are best used to express relatively simple and common construction use cases.  Complicated sequences of |for| and |if| clauses will be rarely seen and hence less understandable than the equivalent explicit looping expansions. 

> 
> 2.  These comprehensions are permitted:
>        [EXPR for x of obj if a, b, c]
>        [EXPR for x of obj if x = 3]

see separate response

...

> 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.

For-of was introduced to be a more generalizable equivalent to for-each-in.   I don't think we want to go back on that design decision and it isn't clear in the above suggestion what the alternative would be to
   [x for x of keys(obj)]  // or [x for x of obj.keys()]
Is it
  [x for keys( x of obj)]
????

There seems to be all sort of complications for the form you suggesting.  For example, must the function that is called be identified by a PrimaryExpressioin?  Is only one argument allowed? Is the binding declaration of "x" always the first argument position. Is something the the |of| keyword in a parameter posEtc.

I find it bad enough that the Expression that references the for clause bound identifiers is to the left of the bindings.  I think burying bindings in augment positions would make things even worse.

Allen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20120922/59d7ef6e/attachment.html>


More information about the es-discuss mailing list