Generator support for CoffeeScript 1.9
The new CoffeeScript 1.9 release has introduced long awaited support for generators, improved parsing and error reporting, as well as fixed REPL compatibility with the latest version of Node and io.js.
The CoffeeScript 1.9 release is the hit a lot of developers have been waiting for, with generator support that alleviates the pain of callbacks and helps with writing async code. With this newly added support for generator functions, developers can just use a yield command.
Happy to yield
The issue of adding yield support was brought up in 2013 and has now been included as part of a progressive approach suggested by Jeremy Ashkenas, that considers new features as soon as they begin to work in browsers (and on Node). The example of generator use on the CoffeeScript website looks like this:
perfectSquares = -> num = 0 loop num += 1 yield num * num return window.ps or= perfectSquares()
On top of ES6 generator support, other features added to the new release include:
- More robust parsing and improved error messages for strings and regexes — especially with respect to interpolation.
- Changed strategy for the generation of internal compiler variable names. This means that @example function parameters are no longer available as naked example variables within the function body.
- Fixed REPL compatibility with latest versions of Node and io.js.
If you had written tests, you could check if it affects you. Take this little piece of code:example = "new" obj = method: (@example) -> console.log(example) obj.method "old"
In 1.8 this would print
old. In the new version, this prints
In the older version,
@examplewould be translated to
examplein the method parameters. So you’re accessing
obj.method‘s function parameter in the old version.
In the new version you’re accessing the
examplevariable of the outer scope.
a.examplestill gets set to
"old"in both cases.
CoffeeScript’s complete changelog is available to view here.