4 ms·
Presumably the author of Squirrel agrees with (one of) the authors of Lua: "Local by default is wrong" [1]. "The problem is that without local declarations, yo
by bakery2k 10y ago
Presumably the author of Squirrel agrees with (one of) the authors of Lua: "Local by default is wrong" [1].
"The problem is that without local declarations, you cannot say where the variable is local to" [2]. Take Python, for example, where "local by default" means "local to the current function". Variables are created in the current function's scope on assignment, which causes 2 problems:
1. It is awkward to modify variables in outer scopes, because an attempt to assign to them creates a new variable. Python works around this with the `global` and `nonlocal` keywords.
2. Python does not support variables which are local to the current block, for example, the body of a loop.
[1] http://lua-users.org/lists/lua-l/2006-10/msg00056.html http://lua-users.org/lists/lua-l/2006-10/msg00056.html
[2] http://lua-users.org/lists/lua-l/2006-10/msg00063.html http://lua-users.org/lists/lua-l/2006-10/msg00063.html
- to3m 10y agoIf Lua did the decent thing and insisted that you declare every variable first, it wouldn't have this stupid problem. Squirrel should probably do it, too. Python certainly should! (Even Javascript, misguided as it is in so many respects, has strict mode!) This sort of static checking massively increases the amount of code you can look after without going insane. Add static types too, and you're even better off. (Last time I used Lua, I modified it so that all variables had to be introduced before their first use, either using `local', or a new `global' keyword. More static checks, and no more accidental globals.)
- technomancy 10y agoI would love to have something like Dialyzer to do static checks for Lua, but luacheck is a lot better than nothing: https://github.com/mpeterv/luacheck https://github.com/mpeterv/luacheck
- bakery2k 10y agoInteresting that hajile and you both dislike global-by-default, but your proposed alternatives are essentially opposites. hajile would prefer local-by-default and fewer declarations, and you would prefer nothing-by-default and more declarations...
- looki 10y agoLua's metatables are very powerful, and there's actually a tiny implementation of "strict mode" for Lua [1]. I do agree that static checks like these would help the language for serious applications. I recently got back into Lua a bit and the first error I made was omitting "local". [1]: http://metalua.luaforge.net/src/lib/strict.lua.html http://metalua.luaforge.net/src/lib/strict.lua.html