-
Notifications
You must be signed in to change notification settings - Fork 4
CS2 Discussion: Output: Fix the scoping of the for loop #63
Comments
#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! |
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. |
@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 |
Especially if #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 |
@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? |
@mitar Ah, right. But there's still a problem: after the loop, you'll print the wrong value of |
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 |
Migrated to jashkenas/coffeescript#4956 |
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: