michaelficarra/CoffeeScriptRedux

`for [a..b] then c`

lydell opened this issue · 3 comments

for [a..b] then c is valid in jashkenas/coffee-script, although it is not documented.

$ coffee -bpe "for [a..b] then c"
var _i;

for (_i = a; a <= b ? _i <= b : _i >= b; a <= b ? _i++ : _i--) {
  c;
}


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

That it is legal doesn't mean it's intentional, very useful or clear. The c part of the expression gets evaluated the appropriate number of times and the value of the expression is a list with the same number of undefined values:

coffee> for [ 0 .. 3 ] then console.log 'x'
x
x
x
x
[ undefined,
  undefined,
  undefined,
  undefined ]
coffee>

It gets worse if you happen to place this as the last line into a function, because then implicit return insertion kicks in and will make that list the return value of the function, which is very probably not what you intended. Do you have any real use case?

The feature is intentional and nowadays it is documented as well.

Is it? The closest i seem to be able to find is, under "Operators and Aliases" on the CoffeeScript homepage, the remark that "Instead of a newline or semicolon, then can be used to separate conditions from expressions, in while, if/else, and switch/when statements." for is not mentioned here, though.