r/programming • • Dec 22 '11

The Problem with Implicit Scoping in CoffeeScript

http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/
83 Upvotes

116 comments sorted by

View all comments

Show parent comment

2

u/munificent Dec 23 '11

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.

Right, but in Dart you get a fresh i for each loop iteration.

The "try Dart" page disagrees with your snippet.

It's a bug. We have two loops: C-style and for-in. I believe that with the latest spec, both create fresh loop variables for each iteration. The VM does the right thing here. DartC (which is what "Try Dart" uses) and frog (the new Dart->JS compiler which will eventually replace Dart) don't. They do the right thing with for-in loops, but not C-style for loops.

Have you guys changed the scoping semantics of C-style for loops?

Yeah, I think so, but only if you declare the variable inside the loop. I think this is more consistent with for-in loops, and the fact that those don't create fresh variables each time is a very frequent source of confusion for people. (I believe Eric Lippert described it as the most frequently-reported bug in C# that isn't a bug.)