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

CS2 Discussion: Output: Fix the scoping of the for loop #4956

Closed
coffeescriptbot opened this issue Feb 19, 2018 · 7 comments
Closed

CS2 Discussion: Output: Fix the scoping of the for loop #4956

coffeescriptbot opened this issue Feb 19, 2018 · 7 comments

Comments

@coffeescriptbot
Copy link
Collaborator

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?

@coffeescriptbot
Copy link
Collaborator Author

coffeescriptbot commented Feb 19, 2018

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!

@coffeescriptbot
Copy link
Collaborator Author

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 (do).

I cannot imagine a use case where you would want to use variable from the for loop inside the closure with the traditional scoping.

@coffeescriptbot
Copy link
Collaborator Author

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 show would fail, printing the old value of x instead of the new one. The behavior would be even worse/less intuitive if the show function changed the value of x (but maybe you want to avoid do in that case...).

@coffeescriptbot
Copy link
Collaborator Author

coffeescriptbot commented Feb 19, 2018

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 do for to output for (let for scoping:

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 do for it will actually compile to javascript, but will always throw an error at runtime:

Uncaught TypeError: (intermediate value)(intermediate value)(intermediate value)(intermediate value)(intermediate value)(...) is not a function

Because it will try to call ([])() an array as if it where an anonymous function.
You can try it out here.

It is important to note that this output change should not effect using do inside a one line for loop:

function1 = -> console.log 'function 1'
function2 = -> console.log 'function 2'

do fun for fun in [function1, function2]

Try it here

We should only change the output for do for if do is immediately followed by 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

Try it out

@coffeescriptbot
Copy link
Collaborator Author

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?

@coffeescriptbot
Copy link
Collaborator Author

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 x.

@coffeescriptbot
Copy link
Collaborator Author

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 let. See coffeescript6/discuss#58 (comment)

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

1 participant