-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Can't manipulate loop indices when closure is present #950
Comments
These are the current proposals to fix this inconsistency: Notice that this will make the loop index immutable between iterations and mutable within them. This was intentional. jashkenas notes that modifying the loop index to change program flow is common and actually used in the compiler itself. Loops like this will have to be rewritten as a |
Yes, as I said, making loop indices consistently immutable would be easy. Making them consistently mutable would be slightly harder (let's see... end |
I think that the behavior in this ticket shouldn't be changed, for the same reason as #954. The dual nature of |
I think that if we don't decide to change this behaviour now, one of two things will happen:
If you were asked what the expected behaviour difference was between Trevor's examples, would you say that there was supposed to be any at all? Because I wouldn't. That's not a feature of a great language like coffeescript. |
I'm with Michael; the implicit "dual nature" of loops is very confusing, and not worthy of CoffeeScript's 1.0 release. I've raised issue 959 to discuss removing the safe block scope feature. |
Thanks for wrapping all of this up in #959, Trevor. Closing this ticket in favor of that one. |
gives you
but, if you define a function within the loop,
gives you
because the code from the loop is moved to a function, making it impossible to modify the original index.
Surely this behavior needs to be made consistent? The simplest solution would be to make loop indices always be immutable, by having the declared index variable always be a copy of the true index variable. The other solution, making loop indices consistently mutable, would be achievable either through a tricky set of
return
s, or by moving_fn
inside of the loop and not passingindex
as an argument.The text was updated successfully, but these errors were encountered: