3 ms·
The worst thing about CoffeeScript is that you are used to having scoping work a different way? Let's extend your example: >>> a = 1 >>> def foo():
by tghw 13y ago
The worst thing about CoffeeScript is that you are used to having scoping work a different way?
Let's extend your example:
>>> a = 1
>>> def foo():
print a
>>> foo()
1
So Python does take from the outer scope?
>>> a = 1
>>> def foo():
a = 2
print a
>>> foo()
2
Or not?
>>> a = 1
>>> def foo():
print a
a = 2
>>> foo()
UnboundLocalError: local variable 'a' referenced before assignment
Or it just doesn't work at all?
Next time, pick a better example. The answer to "What does Python do with a variable from outside a function's scope?" is "It depends."
At least CoffeeScript is consistent.
- deleted 13y ago[deleted]
- aaron-lebo 13y agoIt is a bigger issue than lack of familiarity. It can lead to some really unexpected bugs. There is a reason most languages don't do things this way. http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/ http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffe...
- wildgift 13y agoThis is also a problem with javascript. Javascript should do automatic variable shadowing but also throw a warning.
- emn13 13y agoCoffeescript isn't consistent. setter = (arg) -> store = arg store = 'initial' is quite different from store = 'initial' setter = (arg) -> store = arg This is a practical problem since it means that the most commonly used JS encapsulation feature - closures - become harder to write. Suddenly assignments that have no direct influence on each other cannot be blindly reordered (which is a pain), and in particular closures that have a bunch of helper functions defined initially that need to ensure that local variables are set before the helpers, even if those local variables are themselves functions. It's just a mess. Frankly, I think javascript does this part a lot better than coffeescript, especially now that "use strict" exists and you can get some feedback when your scoping assumptions turn out wrong, rather than a silent failure and unexpected behavior.
- coldtea 13y ago>The worst thing about CoffeeScript is that you are used to having scoping work a different way? No, the issue is that he's used to having scoping work properly. Coffeescript behavior corrects a wrong (shadowing) with an even worse wrong. Even worse, as a default it can affect variables outside of the function, so it breaks locality.
- mattdeboard 13y agoI suspect, but don't know, this is different in Python 3, since `print a` changes from a statement to an expression.