Why do generator expressions return generators?

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Sep 6 11:48:05 PDT 2013


On Sep 6, 2013, at 11:18 AM, Brendan Eich wrote:

>> Allen Wirfs-Brock <mailto:allen at wirfs-brock.com>
>> September 6, 2013 11:01 AM
>> 
>> Almost as good, a arrow function with a generator comprehension as its expression body. For example, a factory for a generator with a lexical this binding:
>> 
>> c => (for (p of c) if (p in this) this[p])
>> 
>> not much different from a hypothetical generator arrow function and arguably better :
>> c *=> {for (p of c) if (p in this) yield this[p]}
> 
> There would be no explicit |yield| in the second example, though. (|yield| is an error in any arrow body.)

why no |yield|?  Note that I wrote a { } body for the generator arrow function body, rather than an expression body and that generator functions use explicit |yield| in their bodies. 

Also, if the expression body form had an implicit |yield| in front of the expression (eg, c *=> 42, implicitly yields 42) then then there would be confusion between 

     c => (for (p of c) if (p in this) this[p])  //function that returns a generator that yields this[p]
     c *=> (for (p of c) if (p in this) this[p]) //function that returns a generator that yields a generator

The first form would almost always be what somebody would want. 
> 
> I'm not seriously advocating generator arrow function syntax, mind you!

Yup, I think we're better off without them.

Allen


More information about the es-discuss mailing list