yield and new : SpiderMonkey and the draft Spec
Dmitry A. Soshnikov
dmitry.soshnikov at gmail.com
Tue Mar 15 08:42:31 PDT 2011
On 15.03.2011 17:58, John J. Barton wrote:
> On 3/14/2011 10:08 PM, Brendan Eich wrote:
>> On Mar 14, 2011, at 11:50 PM, John J. Barton wrote:
>>
>>> On 11:59 AM, Brendan Eich wrote:
>>>> However, there's no way for a generator function to return that
>>>> instance, because a generator function always implicitly returns a
>>>> fresh generator iterator when invoked. It could store |this| in the
>>>> heap, and whatever value |this| receives is saved as part of the
>>>> shallow continuation capture by the generator.
>>>>
>>> The implicit return of a fresh generator iterator makes the example
>>> confusing.
>> It's just like Python.
>
> and Python is perfect? Even successful features have room for
> improvement.
>
Pyhton's and JS's implementation is just a one of. But for those who
work with such an implementation, there should be no a big confusion.
Lua in contrast e.g. uses a special `coroutine.create(generatorFn)`
(http://lua-users.org/wiki/CoroutinesTutorial) -- similarly to your
proposal. But you may do it yourself if it's confusing
function Generator(fn) {
return fn();
}
let g = new Generator(function() {
yield 1;
yield 2;
});
g.next(); 1
g.next(); 2
From JS/Python's viewpoint, this may be considered as superficial thing
-- if you know that the function is a generator function, be prepared
that the _call_ creates the generator object. The other call to the same
generator function, creates a fresh g-object. And so on. There is no
confusion as it would be that the first call create the g-object, and
the other calls already yield, no. For yielding `next` method is.
P.S.:
A small change, e.g. can be to make next as a getter since it doesn't
accept arguments.
g.next; // 1
g.next; // 2
But, it's a cosmetic and actually not so needed change.
Dmitry.
> jjb
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
More information about the es-discuss
mailing list