Nannying (was: Array comprehension syntax)
Jason Orendorff
jason.orendorff at gmail.com
Wed Sep 26 07:02:42 PDT 2012
On Wed, Sep 26, 2012 at 8:22 AM, Andreas Rossberg <rossberg at google.com> wrote:
>> Separate question to you: (for|let|if)+ is what Jason championed, are you on
>> board?
>
> Right, I see no reason to artificially restrict the syntax to specific
> cases, especially given that it wouldn't make the expansion any
> simpler. So I also favour (for|if)+ or (for|let|if)+ (having let makes
> sense, although it probably isn't super important).
It isn't really. The sort of code where you want it is like:
[x for e of elements
let x = get_some_data_out_of(e)
if x]
It doesn't come up very often, but when it does, if you can't use let,
the workarounds are things like:
// use "for-of" to mean "let"
[x for e of elements
for x of [get_some_data_out_of(e)] // ಠ_ಠ
if x]
// go ahead and repeat yourself
[get_some_data_out_of(e) for e of elements
if get_some_data_out_of(e)] // ಠ益ಠ
// declare a temp variable, assign in the if-condition
var x;
[x for e of elements
if x = get_some_data_out_of(e)] // (ಥ﹏ಥ)
// use "for-of" to mean "let" in a different way
[x for x of [get_some_data_out_of(e) for e of elements]
if x] // ⊙﹏⊙
The main drawback of comprehensions is the temptation to get "clever".
It might seem that providing 'let' in comprehensions would exacerbate
that, but on balance I think it actually helps.
-j
More information about the es-discuss
mailing list