I'm new to Lua. I noticed the following pattern in Lua. But I'm not understanding why it's happening.
Here, a
is a global variable and it's accessible inside the foo
function:
function foo()
print(a)
end
a = 10
foo() -- 10
But if we declare a
as local it's not any more accessible inside the function foo
:
function foo()
print(a)
end
local a = 10
foo() -- nil
Why the global variable is accessible inside the function while the local is not?
CodePudding user response:
That's because global variables work differently: Local variables abide by lexical scoping. This means that variable visibility is intuitive: The variable is visible only after it has been declared; it is also visible in nested blocks (such as the function block) or other control flow statements (ifs or loops).
In
function foo()
print(a)
end
local a = 10
foo()
prints nil
because the local variable a
isn't visible yet; the function accesses the global variable _G.a
at run time instead, which evaluates to nil
. Setting the the global variable a = 10
is equivalent to _G.a = 10
or _G["a"] = 10
. When foo is run, it indexes the global table using _G["a"]
to find the value for the variable a
which yields 10
as expected.
TL;DR: Visibility of global variables depends on order of execution since global variables are just syntactic sugar for accessing a table under the hood; visiblity of local variables abides by lexical scoping - variables must be declared before use
(actually foo
indexes the upvalue _ENV
which is _G
by default if it was not assigned to, but for the sake of simplicity I'm using _G
above)
To use a local variable, which is often recommended both for performance & readability reasons, simply declare it before the function:
local a
function foo() print(a) end
a = 10
foo()
local variables inherited by functions are called upvalues in Lua.