How would shallow generators compose with lambda?

Brendan Eich brendan at mozilla.com
Thu May 14 22:11:01 PDT 2009


On May 14, 2009, at 6:04 PM, Waldemar Horwat wrote:

> Brendan Eich wrote:
>> On May 14, 2009, at 1:47 PM, Waldemar Horwat wrote:
>>> This whole thing is another nail in the coffin of generators.   
>>> Generators are the root of the troublesome longjmp's which don't  
>>> play well with others.
>> Are you talking about generators, or lambdas?
>
> Generators.  They cause control flow to jump around between two  
> independent functions.
>
> THe same problem occurs without lambdas:
>
> function gen(arg) {
> try {
>   yield 1;
>   yield 2;
> } finally {
>   alert("Finally called up to three times?!");
> }
> }
>
> That finally will be called either 1, 2, or 3 times depending on  
> what the caller does, which violates the principle that finally is  
> called exactly once, no matter how you leave a scope.

Did you actually test this? Firefox 2 and up work the same.

js> g = gen()
[object Generator]
js> g.next()
1
js> g.next()
2
js> g.next()
typein:6: ReferenceError: alert is not defined
js> alert=print
function print() {
     [native code]
}
js> g = gen(42)
[object Generator]
js> g.next()
1
js> g.next()
2
js> g.next()
Finally called up to three times?!
uncaught exception: [object StopIteration]

Exactly one finally.

/be



More information about the es-discuss mailing list