3 ms·
You get past most of these syntax nitpicks after a while. I wrote plenty of Lua at my previous job, but every once in while would still forget about globals by
by joaomsa 13y ago
You get past most of these syntax nitpicks after a while.
I wrote plenty of Lua at my previous job, but every once in while would still forget about globals by default. I really would love to know why they went ahead with that design decision, when most languages take a far more conservative approach to variable scoping.
- aDevilInMe 13y agoIt is due to the implementation used for lexical scoping. With 5.2, I actually consider it wrong to think Lua code has a global scope and it has no global get or set VM op instructions, there is however an environment. The environment's default name being _ENV and _ENV can also be local. _G is normally, in 5.2, an entry in _ENV that points to back to _ENV.
- zrail 13y agoBecause the original intention for lua was as a simple extension language for non-professional programmers[1] [1]: http://www.lua.org/history.html http://www.lua.org/history.html
- kazagistar 13y agoStore the environment in "global" table. Make a new environment. Point the __index of the new environment at the global table. Make __newindex of the new environment a function that throws an error. Congradulations, you have metaprogrammed lua to only set globals explicitly. Good job!