Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lua, is there a difference between local functions declared with and without the "local" keyword?

Tags:

scope

lua

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.

like image 532
Aiken Drum Avatar asked Jun 26 '18 21:06

Aiken Drum


People also ask

What is the difference between a function and a local function in Lua?

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.

What does local function mean in Lua?

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.

What's the difference between local function and function?

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.

Do you need to declare variables in Lua?

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.


2 Answers

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.

like image 147
Colonel Thirty Two Avatar answered Oct 19 '22 19:10

Colonel Thirty Two


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!

like image 22
brianolive Avatar answered Oct 19 '22 19:10

brianolive