Day 2 meeting notes

Oliver Hunt oliver at apple.com
Fri Jul 30 15:49:11 PDT 2010


On Jul 30, 2010, at 3:35 PM, Brendan Eich wrote:

> On Jul 30, 2010, at 3:02 PM, Oliver Hunt wrote:
> 
>> On Jul 30, 2010, at 2:56 PM, Brendan Eich wrote:
>> 
>>> To avoid this confusion you can add new syntax (|for each| or whatever, doesn't matter). I've argued in recent posts that it is better from a global and long-term point of view to reform for-in after Python, than to condemn it and grow the language with new and generally more verbose, yet similar, syntax.
>> 
>> It annoys me that this doesn't leave a convenient way to iterate arrays, for (... in someArray) will forever do the braindead thing.
> 
> Me too, and I've seen people (myself included) make the mistake of writing
> 
> for (i in [0,1,1,2,3,5,8,13]) ...
> 
> wanting the values not the keys.
> 
> 
>> If we look to pythonic behaviour we see that python's for..in syntax has always iterated arrays by value rather than index.
> 
> Indeed.
> 
> 
>> By overloading for(in) we are effectively saying that there will never be a simple way to iterate arrays by value directly, because no one can even extend the builtin array type be have a generator for iteration because doing so would be too fragile.
> 
> This "never be a simple way" is not true in JS1.7+:
> 
> js> Array.prototype.__iterator__ = function () { for (let i = 0; i < this.length; i++) yield this[i]; };
> (function () {for (let i = 0; i < this.length; i++) {yield this[i];}})
> js> for (v in [3,4,5]) print(v)
> 3
> 4
> 5
> 
> The unstratified, ugly-named __iterator__ meta-method is the getter or factory for finding or creating an appropriate iterator. I use a generator, since it is the simplest way of writing such a factory.

I recognised that was possible -- the problem i was saying is that you can't do that due to it polluting the global array prototype in away that effects language semantics

I'll need to read the spec behaviour more carefully before i'm willing to go too deeply down that rabbit hole though.

--Oliver



More information about the es-discuss mailing list