CoffeeScript: I Love You, I Love You Not.

I have a completely dysfunctional relationship with CoffeeScript

Every time I go back to it things are great. I swoon at its brevity,-> functions, list comprehensions and convenient array slicing.

But then something like this happens:

TypeError: number is not a function

and I'll look at my code:

someFunction someVariable, someOtherVariable -1  

and I'll scratch my head, and I'll look at my code again and eventually, completely baffled, I'll take a look at the JavaScript output and it reads:

someFunction(someVariable, someOtherVariable(-1))  

Now, it's clear what's going on here. CoffeeScript has decided that someOtherVariable -1 must be a function call to someOtherVariable with the argument -1. It's also clear to anybody who can tie their own shoelaces that that isn't what I want to do.

So, I take a guess, and alter my code to read

someFunction someVariable, someOtherVariable - 1  

and hey presto, everything works again. Great! But let's take a second look.

This code:

someFunction someVariable, someOtherVariable -1  

compiles to something completely different to this code:

someFunction someVariable, someOtherVariable - 1  

Hey CoffeeScript, ambiguous much?

And I haven't even mentioned the horrible scoping, less than spectacular debugging experience – sorry but source maps just don't work as nicely – or bizarre choices made for the implicit return.


This time around we've fallen out over generators. Generators are a new feature coming in ECMAScript 6, but are available now in Node.js using the --harmony switch. On their own, generators are simply a nice feature but a little work can turn them into coroutines, letting you say goodbye to callback hell in a far more comprehensible style than promises. (Not that that is intended as a criticism of Q, I'm a big fan.)

Now, given that "CoffeeScript is a little language that compiles into JavaScript." (1) you would think that generators would be pretty simple to implement, and to be fair to the CoffeeScript developers, they are discussing it but there appears to be some controversy over the syntax. Should it be *->, -*>, ->*, or should any function containing a yield statement compile to a generator? This apparently thorny issue has now been under discussion for almost a year so I guess it's back to the land of curley braces for me, and the cycle begins again.

comments powered by Disqus