Preface: As far as I can see, the docs on the website don't really speak to this, and I haven't found anyone else even asking the question, so I'm pretty sure these two forms are identical, but I want know if anyone knows for certain.
Given this Lua code:
function f()
function a() ... end
local function b() ... end
...
end
Is there any functional difference between a()
and b()
? I'm speaking in terms of performance, access, caveats, anything at all. Like, in the end, do they both have exactly the same underlying representation at runtime?
I suspect there isn't any difference, but I'm not sure, and that bugs me. I know a()
is scoped to the enclosing function f()
, but I'm not sure if that truly makes it a local variable in terms of how things function under the hood. With b()
, I can be certain.
We know from the official docs that my definition of b()
above is syntactic sugar for this:
local b
b = function() ... end
I'm tempted to believe that, even without the local
keyword in my definition, the final, de-sugared definition of a()
would also follow exactly that format, including the local a
part.
I just feel like I can't assume this.
the only difference is that one is global and one is not. This means that a global function can be referenced throughout the entire script while local functions only can be referenced in the same block of script.
When we store a function into a local variable we get a local function, that is, a function that is restricted to a given scope. Such definitions are particularly useful for packages: Because Lua handles each chunk as a function, a chunk may declare local functions, which are visible only inside the chunk.
In a function file, which contains only function definitions, local functions can appear in the file in any order after the main function in the file. In a script file, which contains commands and function definitions, local function must be at the end of the file.
Global variables in Lua do not need declarations. Although this is handy for small programs, in larger programs a simple typo can cause bugs that are difficult to find. However, we can change that behavior if we like.
function a() end
in your code block assigns global a
when the function is ran*, while b
remains local to the function.
Perhaps this code segment will illustrate things better:
function f()
function a() end
local function b() end
end
print(a, b) -- nil, nil
f()
print(a, b) -- function: 0xdeadbeef, nil
So to avoid polluting the global environment, you should still use local
inside of a function.
* Unless you declared a
local at some other scope above f
, in which case a
will keep its scoping.
This is not a complete answer to your question, but the most accessible look under the hood of Lua is here written by the creator of Lua. There is some discussion of local variables, but I don't think it answers your question exactly. The section on "external locals" versus explicitly declared locals is informative.
EDIT: And here too. I'll have to read this one again to see if it answers your question. If you beat me to it, do share what you learn!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With