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

Claude Pache claude.pache at gmail.com
Wed Jun 12 10:56:42 PDT 2013



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.

> 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).

–Claude

> 
> /Andreas


More information about the es-discuss mailing list