some generator issues

Yuh-Ruey Chen maian330 at
Sun Apr 22 01:12:00 PDT 2007

There are a couple of issues I see with the generator proposal.

1) What happens when send(x) is called on a newly created generator? In
Python, this results in an exception unless x is None.

2) What happens when a generator calls next/send on itself e.g.

var $gen;
function foo() {
    $;   // what should this do?
$gen = foo();

In Python, this results in an exception.

3) Is there any way to get a generator from within a generator function?
One use case for this would be asynchronous callbacks (can't synchronous
because of issue 2 noted above):

// client calls this function somewhere
function clientFoo() {
    let serverResponse = yield (server.asyncProcedure(clientFooGenerator));

// server.asyncProcedure calls this once it's finished, passing
clientFooGenerator for gen
function serverCallback(gen, retvals) {

The only way to do this right now is to wrap the generator function
within a function that introduces the iterator to the generator
function's scope:

function foo() {
    var $gen = function() {
       // generator code that can use $gen
    return $gen;

This is fairly inefficient and is a hassle to do. Other methods rely on
some sort of generator manager (shift the responsibility to the code
handling the generators rather than within them) or using global
generator vars (which would force the generators to be specific to the
script containing those vars), both of which aren't ideal in the above
use case.

I'm unaware of a Python solution to this issue.

- Yuh-Ruey Chen

More information about the Es4-discuss mailing list