Re: Default iterator

2012-06-25 Thread David Herman
On Jun 20, 2012, at 9:37 PM, Erik Arvidsson wrote:

 In our previous discussion I had come to the understanding that the
 default iterator would use a private name. The main benefit for using
 a private name is to not pollute the property name space.
 
 import iterator from '@iter';
 myObject[iterator] = function*() { ... };
 
 If this was the ordinary property, iterator, it might break existing
 code that already use iterator in some other way.

That was originally decided when we were using it to modify the behavior of 
for-in loops, but since then we decided to leave for-in alone and create for-of 
instead. This changes the cost trade-offs. Since for-of is a new thing under 
the sun, the idea is that you should only use it for objects that are known to 
be iterable -- because otherwise it throws. Objects are not iterable by default 
(in particular, Object.prototype.iterator doesn't exist).

So the chance/cost of potential name conflicts is much lower than it was when 
the iterator protocol extended for-in, because there it would cause existing 
loops, that weren't written with iterators in mind, to break.

 var obj = LibraryX.getObject();
 obj.iterator().forEach(...);

Maybe a better example is a non-thunk obj.iterator. But this is probably 
relatively rare, and you just wouldn't use obj in a for-of loop. You could 
either change the existing API or compatibly add a new iterable property like 
obj.elements.

 It also breaks objects as maps since any string can be used as key.

It doesn't really break, it just means you shouldn't use them as iterables. But 
keys(obj), vals(obj), items(obj) are better anyway since they let you pick 
which things you want to iterate over. Also Map.

Dave

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Default iterator

2012-06-21 Thread Jason Orendorff
On Wed, Jun 20, 2012 at 11:37 PM, Erik Arvidsson
erik.arvids...@gmail.com wrote:
 In our previous discussion I had come to the understanding that the
 default iterator would use a private name. The main benefit for using
 a private name is to not pollute the property name space.

Can you give an example of code that would be harmed by this pollution?

It's not pollution to add a useful method to Array.prototype. ES5 added several.

 If this was the ordinary property, iterator, it might break existing
 code that already use iterator in some other way.

 var obj = LibraryX.getObject();
 obj.iterator().forEach(...);

This code won't break.

If anything the new proposal makes it easier to change LibraryX to
support iteration: just add a suitable next() method to its iterator
object.

 It also breaks objects as maps since any string can be used as key.

Again, please give an example of code that would break.

Note that the decision was already made that there would not be an
iterator method on Object.prototype, so objects-as-maps will be
iterated using a helper function:

var obj = JSON.parse(data);
import keys, items from '@iter';
for (let k of keys(obj)) { ... }
for (let [k, v] of items(obj)) { ... }

-j
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Default iterator

2012-06-21 Thread Allen Wirfs-Brock

On Jun 20, 2012, at 9:37 PM, Erik Arvidsson wrote:

 Jason Orendorff updated the iterator proposal a bit. Thank you Jason.
 I'd like to point out an issue with it though.
 
 In our previous discussion I had come to the understanding that the
 default iterator would use a private name. The main benefit for using
 a private name is to not pollute the property name space.

Well, the ES6 spec. draft uses a private name, so I don't know if editing the 
wiki proposal will have any effect :-)  If somebody things the spec should 
change there probably should have a bug filed against it and a subsequent 
discussion.




___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Default iterator

2012-06-20 Thread Erik Arvidsson
Jason Orendorff updated the iterator proposal a bit. Thank you Jason.
I'd like to point out an issue with it though.

In our previous discussion I had come to the understanding that the
default iterator would use a private name. The main benefit for using
a private name is to not pollute the property name space.

import iterator from '@iter';
myObject[iterator] = function*() { ... };

If this was the ordinary property, iterator, it might break existing
code that already use iterator in some other way.

var obj = LibraryX.getObject();
obj.iterator().forEach(...);

It also breaks objects as maps since any string can be used as key.

-- 
erik
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss