Re: Why can’t for-of be applied to iterators?
rossberg at google.com
Wed Jun 12 17:28:22 PDT 2013
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). 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.
More information about the es-discuss