Why do generator expressions return generators?

Brendan Eich brendan at mozilla.com
Fri Sep 6 09:06:28 PDT 2013


> Domenic Denicola <mailto:domenic at domenicdenicola.com>
> September 6, 2013 8:48 AM
>
> I mean, that's true, but *why* is that true?

See my reply just sent.

> What is the value of allowing you to send in values via `.next(v)`, or 
> send in exceptions via `.throw(e)`?

An iterator does not reject .next calls that pass a value.

As for .throw, don't do that unless you want what you get. "Doctor, it 
hurts when I ...."

> Why not just make them sugar for creating custom iterable-iterators 
> without shallow coroutine capabilities?

Because we need the shallow continuation to hold the implicitly declared 
let bindings induced by the for heads.

/be
>
>
> Brandon Benvie <mailto:bbenvie at mozilla.com>
> September 6, 2013 8:21 AM
>
>
> I don't think you're missing anything. They seem to be more accurately 
> described as Iterator Expressions than Generator Expressions. It might 
> be interesting if you could use yield inside them, which would then 
> make sending a value in useful. But without that they don't expose any 
> generator interface externally.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
> Domenic Denicola <mailto:domenic at domenicdenicola.com>
> September 6, 2013 7:44 AM
> Why can't they just return iterable-iterators?
>
> Consider:
>
> ```js
> const g = (for (x of [1, 2, 3]) x * x);
>
> g.next(); // returns { done: false, value: 1 }
> g.next(5); // ???
> g.throw(new Error("boo!")); // ???
> ```
>
> As far as I can tell from the desugaring at 
> http://wiki.ecmascript.org/doku.php?id=harmony:generator_expressions, 
> the argument to `next` is completely ignored, and doing `throw` will 
> bubble out so that `g.throw(new Error("boo!"))` is equivalent to 
> `throw new Error("boo!")` except that the former will have some 
> implementation stack frames at the top of the stack trace.
>
> What does having these be generators buy us? I am almost sure I'm 
> missing something.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>


More information about the es-discuss mailing list