3 ms·
Every once in a while, I used to think, "What about Lua? Wouldn't Lua be nice?" Then I would start reading the docs, and stop when I came to the "local" keywo
by klodolph 17y ago
Every once in a while, I used to think, "What about Lua? Wouldn't Lua be nice?" Then I would start reading the docs, and stop when I came to the "local" keyword. Ick. Variables are global by default, you have to explicitly label local bindings. If any scope is the default, it should be local scope. Accessing an uninitialized variable is also not an error. Chalk this up to personal taste, but this reminds me too much of bad experiences in bash scripting. The languages I prefer: Python, Haskell, C, Scheme, Common Lisp... never play so fast and loose with where your variables go.
- DrJokepu 17y agoI agree. Variables being global by default (without the var keyword) has been the source of countless bugs in JavaScript.
- silentbicycle 17y agoWhile you're right that "global by default" is usually a problem, it isn't in Lua, because Lua also lets you restrict the global scope (with i.e. setfenv). Generally, the "global" scope you get by default is "global to the current module", i.e., publicly exported from a module. It's still not a problem if your whole program is in one file, though. Since the global environment is just another table, you can set its metamethods to warn about any accidentally global vars. This is an example in PiL, and strict.lua is included in the standard Lua distribution. require "strict", problem solved. The Lua designers know that unrestricted global-by-default vars cause a lot of pain in Awk, but defaulting to local scope for everything instead can make using closures awkward (as in Python), since it's difficult to know automatically where variables are local to. In the other languages you mention, C doesn't have proper lexical scoping, Scheme and Common Lisp have fully parenthesized syntax (and thus no scoping ambiguity), Python's closures are awkward, and Haskell (and ML) use let blocks and other explicit scope markers. This is a semantic issue in any language where you can nest blocks and the default is a series of statements, not individual statements (i.e., "begin" / "progn" is the default). See also: http://lua-users.org/wiki/LocalByDefault http://lua-users.org/wiki/LocalByDefault