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

Axel Rauschmayer axel at rauschma.de
Tue Jun 11 02:08:29 PDT 2013


> The idea that iterators return themselves from their @@iterator (__iter__) hook has years of actual programmer mileage in Python. I take that experience seriously, compared to arguments from purity that tax the common use-cases.


I’d be happy with either solution, but let’s compare:

AFAICT, the tax is on for-of (and Array.from()): they need to additionally check whether an object is an iterator.

On the other hand, turning every iterator into an iterable puts the burden on people implementing iterators: they have to implement the iterable interface. And it becomes harder to distinguish the interfaces, should you need to [1]. IMO, the purity argument also counts: this technique implies that iterators are a subset of iterables, but that isn’t true: For iterables, each invocation of `iterator` creates a new object, for iterators, it doesn’t.

As an aside, Python 3 uses a special name for the `next` method (it previously didn’t). Maybe ES6 should use a symbol here, too. That would eliminate the concerns about false positives, should for-of ever work with iterators.

[1] http://stackoverflow.com/questions/709084/how-to-tell-the-difference-between-an-iterator-and-an-iterable

-- 
Dr. Axel Rauschmayer
axel at rauschma.de

home: rauschma.de
twitter: twitter.com/rauschma
blog: 2ality.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130611/b9c6e380/attachment-0001.html>


More information about the es-discuss mailing list