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

Brendan Eich brendan at mozilla.com
Wed Jun 12 18:33:46 PDT 2013


Andreas Rossberg wrote:
> On 12 June 2013 23:27, Claude Pache<claude.pache at gmail.com>  wrote:
>> Therefore, I think that "For/of coerces its argument to an iterator" is a wrong mental model, and "For/of accepts either a reusable iterable or a disposable iterator" is a better one. And the implicit invocation of `@@iterator` is better thought as Get-the-snark-I-need-for-doing-my-work, than as Coerce-to-a-disposable-object. (And the fact that iterators are also non-reusable iterables is an implementation detail that don't disturb my mental model, because I don't care, thanks to generators.)
>
> In other words, you want to program with iterables. That is a
> perfectly reasonable thing to want. But unfortunately, with the
> current intermingled protocol, there is no particularly useful
> contract of what an iterable actually does (i.e., where the state
> goes).

No, the contract is perfectly useful and minimal. An array is iterable, 
it has @iterator.

>   Consequently, if you want to write abstractions, you will
> likely be better off basing them on iterators, and leave iterables as
> a mere convenience mechanism for for-of loops over concrete objects.

This does not follow. It flies in the face of boatloads of experience 
with JS and Python (itertools and beyond).

I guess we are going 'round the assertion block. I've seen that building 
before :-|.

/be


More information about the es-discuss mailing list