Why can’t for-of be applied to iterators?

Brendan Eich brendan at mozilla.com
Wed Jun 12 11:14:10 PDT 2013


Claude Pache wrote:
> Le 12 juin 2013 à 18:14, Andreas Rossberg<rossberg at google.com>  a écrit :
>
>> On 12 June 2013 14:12, Claude Pache<claude.pache at gmail.com>  wrote:
>>> <snip>
>>>
>>> On the other hand, I see a small advantage of the "weird" requirement for iterators to be iterables themselves: It allows to discriminate between true iterators and merely  "nextable" objects.
>> And in the spirit of "duck typing", why would you want to do that?
>
> Just so that an error is surely thrown in case I feed a for/of loop with a non-iter{ator|able}. In the spirit of duck-typing, I consider that "having an `@@iterator` method" is a more robust check than "having an `@@iterator` or a `next` method". Not a big deal, however.

I think it is a medium-sized deal :-).

First, it's conceptually cleaner to have one symbol-named property test 
than two, symbol and then string name fallback.

Second, it is indeed more robust in this instance: 'next' is a common 
name, with many contextual meanings and likely uses.

>> And
>> how do you want to treat an iterable with a 'next' method, but whose
>> @@iterator method does not return 'this'? Is that a "true" iterator?
>> (The fact that such a beast can even exist shows how fuzzy those
>> notions are.)
>
> You're right, but I don't expect to encounter such a beast often. Concretely, they are buggy objects that would not receive any special treatment (they are iterable whenever an `@@iterator` method is expected, and they are iterated whenever a `next` method is required).

This just isn't a problem in practice. Let's solve real problems, look 
at cowpaths and nearby cliffs, not try to push boilerplate onto all 
programmers out of an off-target desire to make get- at iterator a "type 
coercion".

/be



More information about the es-discuss mailing list