lua-users home
lua-l archive

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


On Tue, May 12, 2020 at 10:51 AM Andrea <andrea.l.vitali@gmail.com> wrote:
I am trying to understand the rational for global by default and explicit local - I have been reading the lua wiki, lua archive, and all I could find on the topic

I have seen that other languages have had similar discussion, in Python for example the proposal is to keep the local by default and use the "nonlocal" keyword

It's not really global-by-default. It's lexical scoping. When you have a declaration, it applies to any sub-scopes created within that scope. This matches the behavior of the vast majority of programming languages -- Python is something of an oddball in this regard.
 
so it seems that local by default could be done - is there any real advantage in using LOCAL instead of NONLOCAL? 

It could be done, but again: lexical scoping. Using "nonlocal" would make _ENV less elegant and it would make closures more verbose, for no practical benefit.
 
I am thinking about what happen when one omits the keyword; or thinking about what happens if one does not take into account declarations in enclosing scopes

from a very broad point of view, when explicit LOCAL is used:
local x = 1 -- always assign to local
x = 1 -- maybe local, upvalue, global; it is global if x does not exist
_ENV.x = 1 -- always global

when explicit NONLOCAL is used
x = 1 -- always assign to local
nonlocal x = 1 -- maybe upvalue or global; it is global if x does not exist
_ENV.x = 1 -- always global

when referencing x in a formula nothing changes: x can be local, upvalue if no local is found with the sam enam, or global if no local and no upvalue has the same name.

Sure. Semantically they're isomorphic: you can always transform one into the other. But I think that "nonlocal" breaks one thing that I think most people assume by default: reading and writing should be symmetric, but "nonlocal" means that reading a value and writing a value have different semantics by default and you have to explicitly opt in to the symmetric behavior.
 
I take the opportunity to ask another question weakly related to this: I have been reading one of the design flaws in Lua is the fact that referencing variables that do not exist simply returns nil and no error is thrown

but this seems to be wrong because if it is does not exist, when it is referenced the global environment is seached, and access to the global environment can be monitored by metamethods, that is strict.lua - am I right?

Lua doesn't throw errors in general. It uses return status values instead: by convention, a function that wishes to signal an error returns nil and an error message. From this perspective, returning nil for a nonexistent variable is entirely consistent with the standard behavior of everything else in Lua.

And yes, you're right: you can opt into a more aggressive error behavior with strict.lua.

/s/ Adam
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org