-
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
CS2 Discussion: Output: Fix the scoping of the for loop #4956
Comments
From @edemaine on 2016-12-12 19:54 coffeescript6/discuss#62 partially addresses this (but see the limitations). I personally can't imagine a more backwards-compatible fix, but I think we're all open to specific suggestions! |
From @mitar on 2016-12-12 19:58 I mean, we are breaking compatibility with new CoffeeScript, no? I would simply do: if a variable from for loop is used inside any closure inside of it, it is automatically wrapped inside a inline function call ( I cannot imagine a use case where you would want to use variable from the for loop inside the closure with the traditional scoping. |
From @edemaine on 2016-12-12 20:07 @mitar Here's one example: (hopefully not too contrived) for x in array
show = ->
console.log 'Current value of x is', x
show()
... do some stuff, change x ...
show()
console.log 'After the loop, x is', x As I understand your proposal, the second |
From @JavascriptIsMagic on 2016-12-12 23:28 Especially if coffeescript6/discuss#61 is to happen, I would like to propose changing the behavior of array = [1, 2]
value = 'outside'
do for value in array
console.log 'Inside:', value
console.log 'Outside:', value Which would compile to something like: var array = [1, 2]
var value = 'outside'
for (let value, index = 0; index < array.length; index++) {
value = array[index]
console.log('Inside:', value)
}
console.log('Outside:', value) Currently in coffeescript if you try to use any form of
Because it will try to call It is important to note that this output change should not effect using function1 = -> console.log 'function 1'
function2 = -> console.log 'function 2'
do fun for fun in [function1, function2] We should only change the output for Very recently I actually ran into this exact problem, and although my code was much more complex it basically boiled down to: value = 'outside'
[inside1, inside2] =
do (value) ->
for value in [1, 2]
do (value) ->
-> console.log 'Inside:', value
console.log 'Outside:', value
inside1()
inside2()
# Output:
Outside: 'outside'
Inside: 1
Inside: 2 |
From @mitar on 2016-12-13 02:59 @edemaine: No, this should work. I would compile your: for x in array
show = ->
console.log 'Current value of x is', x
show()
... do some stuff, change x ...
show()
console.log 'After the loop, x is', x To: for x in array
do (x) ->
show = ->
console.log 'Current value of x is', x
show()
... do some stuff, change x ...
show()
console.log 'After the loop, x is', x As you see, you can change x as it is. @JavascriptIsMagic: Interesting, I didn't know that let inside for loop in JavaScript now rebinds for every loop. Maybe we should just make this a default? So for loop should always use let? |
From @edemaine on 2016-12-13 03:22 @mitar Ah, right. But there's still a problem: after the loop, you'll print the wrong value of |
From @GeoffreyBooth on 2017-11-25 08:39 Closing as this wasn’t changed for 2. But we should perhaps open a new issue, maybe on the main repo, for potentially providing some non-breaking-change way of declaring a loop iteration variable with |
From @mitar on 2016-12-12 19:51
Should we do in the new version CoffeeScript something about the scoping of the for loop and variables being reused inside the closures? I have been bitten so many times with this. CoffeeScript in general tries to fix such strange things from JavaScript, maybe it is time that this is also fixed?
The text was updated successfully, but these errors were encountered: