How would shallow generators compose with lambda?
Waldemar Horwat
waldemar at google.com
Fri May 15 13:47:53 PDT 2009
Brendan Eich wrote:
> 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.
That behavior is wrong in the case that g gets forgotten after calling next just once. You're silently exiting through a finally clause without calling it.
Waldemar
More information about the es-discuss
mailing list