Nov 18 notes

Maciej Stachowiak mjs at apple.com
Mon Nov 22 23:19:09 PST 2010


On Nov 21, 2010, at 7:05 PM, Brendan Eich wrote:

> On Nov 18, 2010, at 4:08 PM, Waldemar Horwat wrote:
> 
>> Consensus that we should have iterators.
> 
> For this, after all these years (JS1.7 added meta-programmable for-in in 2006), I'm grateful, although I wanted to add something your notes did not report:
> 
> To get consensus, we made a tentative agreement to leave for-in as it was and not enable any meta-programmability of it, consigning it to the historically-underspecified and non-interoperable enumeration mystery-meat status.
> 
> Instead, we did the committee-plays-it-safe thing of inventing new syntax for meta-programmable iteration:
> 
> for (var i : x) ...
> 
> This is a break from Python and JS1.7+ in SpiderMonkey and Rhino -- although it matches very late Java and C++ extensions that are similar (but not the same), and really not relevant to JS.
> 
> Worse, the use of colon in this new for syntax is confusingly similar to long-standing future-proofing intentions around runtime type annotations (aka guards or contracts).
> 
> (BTW, I don't think :: is a good type annotation or guard punctuator, btw -- it's the C++ namespace qualification operator, also specified for namespacing by ECMA-357 (E4X) and used that way in ActionScript 3 (and in ES4, RIP). So I was surprised to see :: used for annotation-like syntax in http://wiki.ecmascript.org/doku.php?id=strawman:guards and http://wiki.ecmascript.org/doku.php?id=strawman:trademarks.)
> 
> for (var i : x) ...      // must be new iteration
> for (var i : T : x) ...  // iteration again, but parsed how?
> for (var i : T in x) ... // for-in with annotated var
> 
> Depending on what T might be, grammatically, this could get sticky for top-down parsers. It is confusing and ugly in any event, IMHO.
> 
> Probably we need to take our time and not rush into a meta-programming-here syntax variant of for-in. I'll not propose anything better right now.

If the colon is less future-compatible than we like, why not:

foreach (var i in x)

While "for" is slightly more common for a loop that iterates a container, "foreach" is also quite common, and indeed is often used to name the construct even in languages where the keyword is spelled "for": <http://en.wikipedia.org/wiki/Foreach>.

This would allow us to reserve the colon, and also avoid a surprising change to the semantics of the existing for..in construct. It could even DTRT for arrays. Also, it will likely be easier for programmers to talk about the distinction between "for..in loops" and "foreach..in loops" than "for..in loops" and "for..colon" loops.

Regards,
Maciej




More information about the es-discuss mailing list