archives

« Bugzilla Issues Index

#3104 — Updates to Generators/Iterators


- Add a prototype for iterators, but do not expose a global Iterator constructor for ES6 (leave that for ES7)
- Between Object prototype and Generator prototype
- Initially empty, but accessible
- Comprehensions in general deferred to ES7


The prototype for iterators should have an @@iterator method with the following semantics: `function @@iterator() { return this }`.

That will replace the corresponding method currently defined on
%{String|Array|Map|Set|Generator|Loader}IteratorPrototype%
(Sections { 21.1 | 22.1 | 23.1 | 23.2 | 26.3 }.5.2.2, and 25.3.1.5)


(In reply to Claude Pache from comment #1)
> The prototype for iterators should have an @@iterator method with the
> following semantics: `function @@iterator() { return this }`.
>
> That will replace the corresponding method currently defined on
> %{String|Array|Map|Set|Generator|Loader}IteratorPrototype%
> (Sections { 21.1 | 22.1 | 23.1 | 23.2 | 26.3 }.5.2.2, and 25.3.1.5)

I generally agree, although this was not discussed at the meeting.


That does seem like a simplification. I don't think we need to wait another 2 months for this.


(In reply to Erik Arvidsson from comment #3)
> That does seem like a simplification. I don't think we need to wait another
> 2 months for this.

I agree. I actually read that suggestion as an implementation detail ie. implementing the update to the spec.


I agree that this is probably a good thing to do. (I was actually thinking about it before Clauds's comment showed up).

My only reservation, is that @@iterator is not currently listed as part of the Iterator interface http://people.mozilla.org/~jorendorff/es6-draft.html#sec-iterator-interface and some people have argued that Iterators, in general, should not be required to support @@iterator. By putting such a method into %IteratorPrototype% along with the expectation that user defined iterators will want to inherit from it pretty much enshrining @@iterator as part of the Iterator interface.

In practice this may not be such a bad thing as for-of essentially requires it. As part of putting this change into the spec. I noted a bug where the iterators returned by some operations in the ES6 spec, for example [[Enumerate]] of string exotic objects, didn't include an @@iterator method. Since those iterator will now inherit from %IteratorPrototype% the bug will just go away if %IteratorPrototype% implements @@iterator.

BTW, this is what I've documented in the spec. for how you can gain access to %IteratorPrototype%:

Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator()))

I have a hard time believing that very many ES6 devs are going to bother to write that to set the [[Prototype]] of any ad hoc iterators they define. Or even:

return {
__proto__:[][Symbol.iterator].__proto__.__proto__],
next() {...body of custom iterator next method ...}
}

If we think that inheriting from %IteratorPrototype% is important for future extensibility, then it might be better to just go ahead and define the global Iterator. Then people could define ad hoc iterator like the above by saying:

return new class extends Iterator{
next() {...body of custom iterator next method ...}
}


That would certainly be nicer for user code


fixed in rev28 editor's draft


fixed in rev28