Search code examples
functionluaforward-declaration

In lua, is there forward declaration?


I wrote quite a lot of functions that call one another, in lua.

Is there, in lua, such a concept as "forward declaration" ?

That would allow me to declare all functions with no implementation and then implement them later. I would then get rid of the order of the functions problem.


Solution

  • yes the visibility goes from top to bottom. You can declare locals with no value.

    local func -- Forward declaration. `local func = nil` is the same.
    
    local function func2() -- Suppose you can't move this function lower.
        return func() -- used here
    end
    
    function func() -- defined here
        return 1
    end