Suggestions for Set

Allen Wirfs-Brock allen at wirfs-brock.com
Tue Oct 2 16:20:10 PDT 2012


On Oct 2, 2012, at 3:55 PM, Brendan Eich wrote:

> Domenic Denicola wrote:
>> From: Rick Waldron [mailto:waldron.rick at gmail.com]
>> 
>>>> This seems like an unfortunate decision, and I couldn't discern the motivation in the minutes. When would I want to use a syntax that fails for iterables? Does [...x] throw for an iterable, or result in something else?
>> 
>>> The direct reasoning for the resolution was:
>> 
>>> "Cannot be both iterable and array-like"
>> 
>> I can't really understand what this is trying to say. It must be because I don't understand "iterable". Doesn't it just mean "responds to for-of"?
> 
> It means (in ES6, SpiderMonkey uses a string property name 'iterator') that the object in question ("is obj iterable?) has an @iterator property, which when called gets or creates an iterator for obj.
> 
>> I would imagine arrays, NodeLists, arguments, and other array-likes all respond to for-of; if they don't, that seriously decreases the utility of for-of!
> 
> Array.from is that an object is either iterable, or failing that, array-like (even if no .length property, ToUint32(undefined) => 0 so no elements). See bottom of https://mail.mozilla.org/pipermail/es-discuss/2012-July/024207.html.

But also see https://bugs.ecmascript.org/show_bug.cgi?id=588 and in particular comment 1 which says:

Did we talk about the implications of of using iterable protocol if the array
has holes?

The proposed standard iterator for Arrays turns holes into undefined.  While
normal array-like iteration skips holes. I would expect that

let a2 = Array.from([,,,,5]);

would produce a new array that was just like the argument array.  But using
iterator protocol would make it just like:

let a3 = Array.from([undefined, undefined, undefined, undefined, 5]);

It seems to me that the right solution, is that if the from argument is a real
Array then Array-like, hole preservation, iteration should be used.  If it
isn't a real Array, then from should first try iterable protocol and if it
isn't there fall back to array-like.

> 
> As Allen's followup points out, array iteration skips holes, to match Array.prototype.forEach. Should spread fill holes or skip them?

Holes need to be skipped if we expect

var arrayWithHoles = [,,2,,,5];
var copyOfArrayWithHoles = [...arrayWithHoles]];

to make an accurate copy.  

> 
>>> It's possible that this could be added to the agenda for november, if Brendan wants to discuss SpiderMonkey's experience implementing it. It seems to me that if they're not having issues then it's at least worth a mention.
>> 
>> Yay!
> 
> I actually do not remember why we went back to only array-like instead of iterable || array-like for spread. Can someone (Rick? Dave? Arv?) recall?
> 
> /be
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121002/cc6883bf/attachment-0001.html>


More information about the es-discuss mailing list