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

3

u/gwillen Dec 23 '11

I realize I'm just some guy on the Internet, but: I don't think the alternative model (in which each iteration of the loop is a fresh scope) is that odd. It matches the model used in lisps, where iteration is supposed to feel like tail recursion so it would be odd if each iteration weren't a fresh scope. It also matches one of the two models available in Perl, which distinguishes between:

for $x (@xs) {
    # $x in the outer scope is getting set in the loop
}

and:

for my $x (@xs) {
    # $x is scoped to the loop body, and is freshly bound on each iteration; you can capture it in a closure and you'll get a fresh binding each time.
}

Thus it would feel natural to me for something like your snippet above to bind i freshly each time through the loop, and for the other behavior to be available by choosing to bind i above the loop. (This does not, I admit, square particularly well with the actual syntax, in which the 'var' or 'let' appears in the initialization section of the loop, and appears to only be run once; but it would be really nice to have some concise way to get these semantics.)

1

u/dherman Dec 24 '11

We're definitely doing that for for-in loops, where the variable is automatically initialized each iteration by the semantics. For that, ES6 will definitely have a fresh binding for each iteration. The question in my mind is the C-style loop, where the programmer is actually mutating the local variables themselves.

1

u/gwillen Dec 24 '11

Ahhhh, I see now. That sounds like a very reasonable distinction to me. Thanks for the response. :-)