A syntax alternative for generators
Contrived example has contrived error. Sorry, this is really not fairly argued or evidence-based.
Generators are the easiest way to write an iterator implementation. Typically iterator implementations are named, reusable. The better way to write anything like the mutable collection snapshot iteration is (from my earlier message):
function collectionIterator(aCollection) { for (let i = 0; i < aCollection.length; i++) yield aCollection[i]; }
function snapshot(aMutableCollection) { return collectionIterator(aMutableCollection.clone()); }
But mutable collections can sometimes have smarter lazily snapshotting iterators, so this is just a sketch. Still this is very much like real JS built on closures and Prototype- and jQuery-like clone/extend utilities.
Prototype in particular is full of this.each(function(...){...}) and Object.keys(...).each(function...) uses. No one writes ad-hoc/non-reusable closure-based iterators, as far as I can see.
s/generator/closure-based/ above makes no difference -- a point I made earlier. You cannot single out generators here any more than closure-based iterator factory functions.
Allen Wirfs-Brock wrote:
I strongly believe we should not go off into uncharted (as in unprototyped and not user-tested, or based on established work in other languages) territory.
There's nothing wrong with using function* (and much right) for generators, compared to alternatives we've already discussed. We can't add a new keyword even with newline sensitivity, and preserve the ability to write generator function expressions. Programmers who know Python don't want a new keyword. And arrows are not binding forms, while generators generally (but not always) want to be function declarations.
I'll be blunt here. You are making up something that is not common enough to impair generators in Python or JS1.7+. We have many years of experience with these languages. This contrived or a-priori worry needs evidence to motivate disrupting ES6 plans, at this point.
That's JS: you have to bind |this| one way or another, just as for any inner function. There is nothing new here.
Indeed trying to make generators different from functions just makes for more non-compositionality. Or assuming this must be the outer |this| just assumes the arrow conclution. Again it is very rare to write a nested generator this way, just to have a prolog that takes a snapshot. Here's a better collection example:
function collectionIterator(aCollection) { for (let i = 0; i < aCollection.length; i++) yield aCollection[i]; }
function snapshot(aMutableCollection) { return collectionIterator(aMutableCollection.clone()); }
By removing the contrived nesting and declaring a reusable collectionIterator, separating concerns, the gravamen of your complaint goes away.
This is a feature, don't break it.
Contrived code has contrived hypothetical common error. Evidence!
No more than anything in the body, if we use the simple and flat semantics Jason advocated.
I really think this evidence-free approach to overturning the generator design, where we've stood on Python designers' and developers' shoulders and prototyped for almost six years, is bad business. I know, it's "just es-discuss", but at this point we have consensus based on the champions model for ES6. What you are doing is disorderly. It would be less disorderly, but still objectionable, to simply cut generators (and default parameters).