Generator (yield keyword) support in CoffeeScript for Node.js
almostobsolete.net
almostobsolete.net
https://github.com/jashkenas/coffee-script/pull/3078/files
If anyone wants to chime in on the pull request itself, input is welcome -- particularly about how to address the conflict with the usual "compiles to JavaScript that runs anywhere" ethos of CoffeeScript. If we're going to start moving past that, "yield" is a fine place to start.
I'd be very interested to see a proposal for how to deal with that moving forward for CoffeeScript. Will CoffeeScript:
1) ignore new ES6 features that have syntactic overlap with existing CoffeeScript features
2) leave the existing syntax alone and create new syntax that targets the ES6 syntax (for/of means CoffeeScript for/of, while for/inside means ES6 for/of)
3) break backwards compatibility and release a CoffeeScript 2 that targets ES6 and doesn't use syntax that could conflict in the future
There may be another option, but I can't think of it. Knowing exactly which of these options CoffeeScript will take will give me a better understanding of the long-term viability of CoffeeScript.
> Knowing exactly which of these options CoffeeScript will take
> will give me a better understanding of the long-term viability
> of CoffeeScript.
Mmm, nothing like the whiff of uncertainty on a summer evening ;) For starters, you needn't worry -- CoffeeScript already being a heavily forked open-source project, I'm sure there will be versions (whether Coco, Live, Iced, Gorilla, or perhaps a cutely named CoffeES6cript) that track each of the options you've listed.But you asked about my proposal. I'm most interested in targeting the useful subset of JavaScript that runs across the popular JS platforms at any given moment -- not what may or may not exist in the future, but lives currently as a spec; and also not features that actually exist, but aren't essential (read, harmful, error-prone, nasty), like getters and setters, or E4X (in my opinion, natch).
So in general, what I'd like to see mainline CoffeeScript do, is adopt useful ES6, ES7, and ES8 features, as soon as they land in enough platforms/browsers to be widely useful (yield may be at this point already, or very soon), and to continue to try and find a pleasing minimalist syntax for writing and reading them. If this means taking the ES* syntax wholesale, that's fine. If it means minting a new (and hopefully, but arguably, nicer) syntax, that's fine too. And if it means breaking backwards compatibility, that's also alright, as the compiled lowest-common-denominator JS output will still be perfectly compatible with the new stuff.
With that said, I'm happy to lose backwards compatibility in lieu of ES6 support. We've already seen forks like IcedCoffeeScript before.
It should be a major jump though, with a migration guide at least. It's similar to the 1.8.7 -> 1.9.2 jump. It's painful, ugly, but for the greater good.
Could you point to some documentation/discussion/blogs/anything that addresses these concerns and provides decent examples of use?
Feel free to message me on Twitter (@olegpodsechin) and I'd be happy to answer any questions you have.
For many use-cases, this is a good enough reason to use them.
Still missing is a syntax to consume these generators, like in "for a in b" or "yield from"
var generator = g1(), result = {}, send;
while(!result.done) {
result = generator.send(send);
send = yield result.value;
}I've almost got yieldfrom working...
See my longer question about this upthread.
> Update: I've created a PR [pull request] for CoffeeScript. I've also just added the yieldfrom keyword...
Maybe this update happened after your comment?
If not, how could you modify the Coffeescript compiler to make that happen? (Preferably without requiring browser makers to make any changes.)
If you did want a generator that didn't yield anything I guess you could write:
myEmptyGen = ->
yield if false
Which is pretty ugly, but it's not something I've needed to do often with generators (in Python at least)https://github.com/almost/coffee-script/commit/67f2347685b45...