Generators improvement idea

# Irakli Gozalishvili (13 years ago)

I have an improvement suggestion for generators. Unless I'm mistaken in spider monkey you yield maybe used as a function:

function g() { yield(1) yield(2) }

for (let $ in g()) console.log($)

It would be cool to take this even further and let users give yield a context specific name.

// yield is passed in function* program(require) { // maybe even alias passed in yield var use = require;

var _ = require('underscore') var template = require('asset!./template.html') var data = require('asset!./model.json')

document.body.innerHTML = _.template(template, data) }

This would loading all assets async but avoid callbacks

This is idea came to mind while thinking how SDK APIs that users commonly complain about can be improved (At the moment it web worker like). There are some more examples

in the "more thoughts" section of the following document: mozilla/addon-sdk/wiki/JEP-Content-scripts

-- Irakli Gozalishvili Web: www.jeditoolkit.com

# Brendan Eich (13 years ago)

This does not work -- yield must be a special form that the compiler can see. An arbitrary call through a funarg cannot be presumed to maybe-yield without imposing unacceptable performance costs on all non-yield calls out of generator bodies.

# François REMY (13 years ago)

While we're at it, I had an idea to allow to avoid the function*() {} syntax: why not "yield [noLineTerminator] return" ?

Since "return" is a reserved word which can only appear as first token of an instruction, enforcing [noLineTerminator] makes sure any recognized use of 'yield return' in ES6 would not be valid in ES5. Also, yield can continue to be used anywhere else as a variable, if needed.

Code sample :

function counter() {
    var i = 0; yield return i++;
}

Another variant would be :

function counter() {
    var i = 0; continue return i++;
}
# Rick Waldron (13 years ago)

On Fri, May 25, 2012 at 2:10 AM, François REMY <fremycompany_pub at yahoo.fr>wrote:

While we're at it, I had an idea to allow to avoid the function*() {} syntax: why not "yield [noLineTerminator] return" ?

This approach expects all future readers of my code to look deep into the function definition to locate a "yield return", instead of the conveniently placed asterisk. Someone else mentioned this here: JSFixed/JSFixed#72

Since "return" is a reserved word which can only appear as first token of an instruction, enforcing [noLineTerminator] makes sure any recognized use of 'yield return' in ES6 would not be valid in ES5. Also, yield can continue to be used anywhere else as a variable, if needed.

Code sample :

function counter() { var i = 0; yield return i++; }

vs. the significantly less crowded:

function* counter() { var i = 0; yield i++; }

Another variant would be :

function counter() { var i = 0; continue return i++;

}

Certainly you don't think piling more semantics on top of continue is a better choice?

# David Herman (13 years ago)

On May 25, 2012, at 5:29 AM, Rick Waldron wrote:

On Fri, May 25, 2012 at 2:10 AM, François REMY <fremycompany_pub at yahoo.fr> wrote: While we're at it, I had an idea to allow to avoid the function*() {} syntax: why not "yield [noLineTerminator] return" ?

This approach expects all future readers of my code to look deep into the function definition to locate a "yield return", instead of the conveniently placed asterisk. Someone else mentioned this here: JSFixed/JSFixed#72

In addition, it means that generators that don't have any yields in them are awkward:

function g() {
    if (false) { yield } // I am a generator, dammit!
}

This might sound esoteric, but it does come up, e.g. when composing multiple generators and deal with base cases. I have been personally bitten by it when working with task.js in SpiderMonkey: I was editing a task to debug it, and I commented out the only occurrence of yield and suddenly inadvertently changed the generator function back to a normal function.