days
-4
-6
hours
-1
-4
minutes
-2
-2
seconds
-3
-5
search
JAX Developers Puzzle deadline approaching: Play for a chance to win a complimentary JAX London 2018 ticket!
Caffeine fix

Generator support for CoffeeScript 1.9

Natali Vlatko
Coffee image via Shutterstock

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.

The supplied updates in CoffeeScript come as we inch closer to the next version of JavaScript ES6, which will feature many new CoffeeScript features as standard. For users confused about the incompatible change for the generation of internal compiler variable names, Stack Overflow provided the following example:

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 new.

In the older version, @example would be translated to example in the method parameters. So you’re accessing obj.method‘s function parameter in the old version.
In the new version you’re accessing the example variable of the outer scope. a.example still gets set to "old" in both cases.

CoffeeScript’s complete changelog is available to view here.

Author
Natali Vlatko
An Australian who calls Berlin home, via a two year love affair with Singapore. Natali was an Editorial Assistant for JAXenter.com (S&S Media Group).

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of