CoffeeScriptRedux icon indicating copy to clipboard operation
CoffeeScriptRedux copied to clipboard

`for [a..b] then c`

Open lydell opened this issue 11 years ago • 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.

lydell avatar Jun 28 '14 13:06 lydell

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?

loveencounterflow avatar Jun 01 '15 06:06 loveencounterflow

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

lydell avatar Jun 01 '15 15:06 lydell

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.

loveencounterflow avatar Jun 01 '15 16:06 loveencounterflow