You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I have a loop which produces inner functions referencing the iterator, those functions will refer not to their own copy of the iterator, but to an variable defined in the outer scope. The following test illustrates the problem:
test "iterator scope", ->
actual = []
expected = [1, 2, 3]
funs = []
for x in expected
funs.push -> actual.push x
funs[0]()
funs[1]()
funs[2]()
eq actual, expected
This produces the following javascript using coffee version 1.0.0:
The loop variable is re-bound to each element of the array and is shared by all the functions produced. When they are called, they all use the last value assigned (in this case, the 'actual' array ends up being [3, 3, 3], instead of the expected [1, 2, 3]).
In previous versions of Coffeescript, the code would actually produce something like this:
Yep, auto-closure-wrapping was removed just before 1.0 because it was broken. A proper fix is possible, but it's a very difficult patch. See #959 for the suggestions (and here's mine).
edit: Also, see the first sentence of the change log for 1.0.0.
When I have a loop which produces inner functions referencing the iterator, those functions will refer not to their own copy of the iterator, but to an variable defined in the outer scope. The following test illustrates the problem:
This produces the following javascript using coffee version 1.0.0:
The loop variable is re-bound to each element of the array and is shared by all the functions produced. When they are called, they all use the last value assigned (in this case, the 'actual' array ends up being [3, 3, 3], instead of the expected [1, 2, 3]).
In previous versions of Coffeescript, the code would actually produce something like this:
Which works correctly.
The text was updated successfully, but these errors were encountered: