r/programming • u/zumpiez • Dec 22 '11
The Problem with Implicit Scoping in CoffeeScript
http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/
83
Upvotes
r/programming • u/zumpiez • Dec 22 '11
View all comments
Show parent comment
5
u/dherman Dec 23 '11 edited Dec 23 '11
Your Dart code does not do what you're thinking it does. It prints 10 repeatedly. Just like JS closures, Dart closures store references to outer variables, not copies. You have bound one single copy of i, which is mutated in each iteration of the loop. If you want to close over different copies, you need to bind a variable inside the loop.
And ES6 will let you do exactly the same thing:
For more info, see the ES6 draft proposal on block scoping: http://wiki.ecmascript.org/doku.php?id=harmony:block_scoped_bindings
Edit: Oh hey, I didn't realize it's you, Bob! The "try Dart" page disagrees with your snippet. Have you guys changed the scoping semantics of C-style for loops? That would be very odd.