Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for [a..b] then c #317

Open
lydell opened this issue Jun 28, 2014 · 3 comments
Open

for [a..b] then c #317

lydell opened this issue Jun 28, 2014 · 3 comments

Comments

@lydell
Copy link
Collaborator

lydell commented Jun 28, 2014

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.

@loveencounterflow
Copy link

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?

@lydell
Copy link
Collaborator Author

lydell commented Jun 1, 2015

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

@loveencounterflow
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants