Why can’t I do this?
if a then print("`a` already exists as a global!") end
a = a or 1
I get this error message :
*** TestPad.lua:10: variable ‘a’ is not declared
*** stack traceback:
*** [C]: in function ‘_error’
*** [string “local mt = getmetatable(_G)…”]:29: in function <[string “local mt = getmetatable(_G)…”]:24>
*** TestPad.lua:10: in main chunk
These are totally valid code chunks in vanilla lua.
Obviously, this has to do with the global environment, but that still mystifiesme, even in vanilla Lua.
My guess is that I’m expecting
to return nil, but clearly, there’s more going on than that.
I’m wondering because I use a global string to load files in a couple of projects. Both projects use the same name for the identifier: _mainroot. To avoid conflicts, I’m stashing_mainrootinside another varaible,_old_mainroot, and then loading it back into_mainrootat the end of the file. But I can’t do that if I can’t do the above.
As a quick workaround, I just added the statement
\_mainroot = '' into the file which requires those two projects.