return when desugaring to closures

Brendan Eich brendan at mozilla.org
Mon Aug 25 21:50:19 PDT 2008


On Aug 25, 2008, at 8:51 PM, Mark S. Miller wrote:

> How important is it that yield be an expression rather than a
> statement? It seems like a sibling of return and throw, so I don't
> think anyone coming to them afresh would be surprised if yield were a
> statement.

This is and was not a clean-slate exercise. We both followed and gave  
feedback to Python in doing this work, because the Python and JS  
communities overlap significantly, and the languages share certain  
stylistic and substantial features (not all, but enough). The goals  
include reusing the Python open-source/python-dev experience and the  
developer brainprint.

yield as an expression form is important for so-called "co-routines",  
where you can call gen.send(val) to resume a generator the val  
becoming the result of the last yield expression; you can also  
gen.throw(exc).

Finally, and this is possibly the most useful, gen.close() will be  
called in JS when a generator used in a for-in construct. With close  
called from for-in, cleanup can be automated that otherwise can't be  
done without Python-style ref-counting/GC, Java-style finalization,  
or worse -- complexities we wish to avoid in any ES standard.

Calling gen.close() on an open generator forces a return from the  
last yield. This has the desirable property that if the yield is in a  
try, any relevant finally clauses will be run. (Return from generator  
throws StopIteration, as usual for iterators; a generator can't  
return a value.) Background reading:

http://mail.python.org/pipermail/python-dev/2006-August/068497.html
http://mail.python.org/pipermail/python-dev/2006-August/068498.html
http://mail.python.org/pipermail/python-dev/2006-August/068450.html

One use of generators, which Neil Mix wrote: "Threads" in JS:

http://www.neilmix.com/2007/02/07/threading-in-javascript-17/

Source to read:

http://www.neilmix.com/demos/js17threading/example.js
http://www.neilmix.com/demos/js17threading/Thread.js

These use send and yield expressions heavily. The es- 
discuss at mozilla.org archives at

https://mail.mozilla.org/pipermail/es-discuss/

best searched with Google site:mail.mozilla.org search, have a number  
of threads tracing the evolution of generators. Besides Igor  
Bukanov's work to simplify the close mechanism by eliminating  
GeneratorExit, Chris Hansen of Google made the crucial proposal to  
automate close only from for-in constructs (not from GC).

Generators are in JS1.7 (Firefox 2). Sugar in the form of generator  
expressions, as in Python 2.4, are in JS1.8 (Firefox 3). My port of  
Peter Norvig's Sudoku solver, which uses genexps:

https://bugzilla.mozilla.org/attachment.cgi?id=266577

I believe Pythonic generators have been the most useful, if not most  
widely used, of the extensions that we have shipped since JS1.5 (ES3  
+ getters and setters).

/be


More information about the Es-discuss mailing list