Skip to content
This repository has been archived by the owner on Feb 19, 2018. It is now read-only.

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

Closed
mitar opened this issue Dec 12, 2016 · 8 comments
Closed

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

mitar opened this issue Dec 12, 2016 · 8 comments

Comments

@mitar
Copy link

mitar commented Dec 12, 2016

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?

@edemaine
Copy link

#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!

@mitar
Copy link
Author

mitar commented Dec 12, 2016

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.

@edemaine
Copy link

edemaine commented Dec 12, 2016

@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...).

@JavascriptIsMagic
Copy link

Especially if #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

@mitar
Copy link
Author

mitar commented Dec 13, 2016

@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?

@edemaine
Copy link

@mitar Ah, right. But there's still a problem: after the loop, you'll print the wrong value of x.

@GeoffreyBooth
Copy link
Collaborator

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 #58 (comment)

@coffeescriptbot coffeescriptbot changed the title Fix the scoping of the for loop CS2 Discussion: Output: Fix the scoping of the for loop Feb 19, 2018
@coffeescriptbot
Copy link
Collaborator

Migrated to jashkenas/coffeescript#4956

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

No branches or pull requests

5 participants