lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



On 08/08/14 05:13 PM, Phill Djonov wrote:
On Fri, Aug 8, 2014 at 12:49 PM, Steven Degutis <sbdegutis@gmail.com> wrote:
I'd love to hear more about how "local by default is a mess".

As Jorge already noted in this thread, "local by default" only makes sense if all variables must be explicitly declared. If you haven't got that property, then you can't locally hide variables declared in prior scopes, which makes for incredibly fragile code.

Since Lua already requires local variables to be declared, there's really not much to change. The only thing that could really be changed is either requiring global variables to also be explicitly declared, or forcing them to be identified as global with some operator-like prefix or a naming convention. But that's different from "local by default" - that's "there are no defaults".

Incidentally, Lua's globals are actually implemented as locals, since a reference to a global variable is just syntactic sugar for _ENV["variablename"], where _ENV is a language-defined local variable which the runtime initializes for you with the table of globals. (And yes, it's a real local, you can shadow it or reassign it like any other variable, and all "global" access from that point on will go through whatever value you change it to.)
So I can local env = _ENV, throw _ENV away (_ENV = nil) and not use strict.lua?

Is there anything written on this topic regarding Lua?


(IIRC this was also linked up-thread, but for some reason gmail isn't letting me view all the messages, so I don't know who to credit.)