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

1

u/alexeyr Dec 23 '11

But if you have variable "x" in two different lexical scopes, you don't know if it refers to the same variable, or two different variables: this depends entirely on existence of "x" in an outer scope. Again, loss of locality.

-1

u/showellshowell Dec 23 '11 edited Dec 23 '11

There is no rolling of dice here. There is no "you don't know". You do know. The scope of "x" is well defined in CoffeeScript, and it's entirely lexical and predictable.

Same x everywhere:

x =2 
f1 = ->
  x = 3
f2 = ->
 x

Different x everywhere:

f = ->
  x = 3
f = ->
  x

You are correct that "locality" is compromised for some narrow concept of locality. The ultimately prevent-you-from-shooting-yourself-in-the-foot programming language would scope variables to a single line. Then we'd never, ever have naming collisions. ;)