lexical for-in/for-of loose end

Brendan Eich brendan at mozilla.org
Mon Jan 30 18:38:52 PST 2012


No! We have a standing agreement to get rid of the awful, 
lazy-grammar-reuse error in ES1 that allows an initializer in for(var 
x=i in o). We do not want this at all for 'let' in either for-in or for-of.

I think we should break unconditionally and forbid =i in for(var x=i in 
o) too, but that is a separate issue. This botch in grammar factoring is 
a (bad) sunk cost that has zero bearing on the fresh let binding per 
iteration idea. It's terrible anti-precedent. Just say no.

/be

> Allen Wirfs-Brock <mailto:allen at wirfs-brock.com>
> January 30, 2012 6:09 PM
> Here is valid ES6 for-in statement:
>
> for (let p=alert("initializing p"+p) in [0,1]) alert(p);
>
> Each iteration gets a fresh p, but does it produce three alerts saying:
> Initializing p
> 0
> 1
> or four alerts saying
> Initializing p
> 0
> Initializing p
> 1
>
> I would expect the first alternative.
>
> However, taking the first alternative, what happens for
>
> let p="outer";
> for (let p=alert("initializing p"+p) in [0,1]) alert(p);
>
> Is the first alert: initializing outer
> or does it throw a reference error for a temporal dead zone violation?
>
> I would expect the reference error.
>
> Essentially the for-in is conceptually wrapped with a block that has 
> an uninitialized binding for p. The initializer of the let declaration 
> is evaluated once in the scope of that block and then the value is 
> discarded because it is inaccessible. For each iterations, a fresh 
> block is created containing a binding for p that is initialized to the 
> current iteration key and then the statement is evaluated in the scope 
> of that block.
>
> Thoughts?
>
> Allen
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list