Coffeescript is easily confused by whitespace. Of the following expressions, all work as expected except the last one. It thinks the last one is a function call, where “a” is the function and “+b” is a parameter. I don’t know what “+b” means. Most parsers would look for a left-hand side for that expression.
x = a + b # Normal addition x = a+b x = a+ b x = a +b # Parses as "x = a(+b)", thinks "a" is a function.
Coffeescript has a nice syntax for closures, similar to C#. However, it is again confused by whitespace when passing functions around.
compose (x) -> f(x), (y) -> g(y) # Parse Error: confused by comma compose (x) -> f(x) # Parse Error: Still confused by comma , (y) -> g(y) compose (x) -> f(x) # Works! It finally parses the comma correctly. , (y) -> g(y) compose ((x) -> f(x)), (y) -> g(y) # Works! Wrapping in extra parens helps the parser.
Comprehensions can either return a collection or a value depending on subtle differences in the syntax.
r1 = e for e in l # (1) Turns into FOR loop with "r1 = e" in the body r2 = (e for e in l) # (2) Each value e is pushed onto a # collection, behaves like map. f1 = () -> e for e in l # (2) f2 = () -> (e for e in l) # (2) f3 = () -> # (1). Adding another line to the function e for e in l # changes the behavior of the loop!! e f4 = () -> # (1) (e for e in l) e f5 = () -> e for e in l # Parse Error: The parser doesn't e # handle whitespace well.
a = 1 # Defines a in global scope f = (a) -> a = 2 # "a" refers to parameter g = (x) -> a = 2 # "a" refers to global h = (x) -> var a = 2 # Parse Error! No way to create a new "a" in local scope.
There are other little issues which are a matter of taste, not functionality. For example, if-then-else statements are weird. It reserves the “then” keyword for one-liners, but doesn’t use it for normal if/else statements. Or with
x = if cond then val1 else val2 x = if cond val1 else val2