A simple question: How do I set the prototype for a function that has not been implemented yet?
I just want to do this, cause I’m referring to a function that does not exist(yet).
In C, we would do something like this:
int foo(int bar);
int myint = foo(1);
int foo(int bar)
{
return bar;
}
How do I do this in Lua (with corona)?
You can’t. Amber’s comment is correct.
Lua doesn’t have a concept of type signatures or function prototypes.
The type of
foois that of the object it contains, which is dynamic, changing at runtime. It could befunctionin one instant, andstringorintegeror something else in the next.Conceptually Lua doesn’t have a compilation step like C. When you say “run this code” it starts starts executing instructions at the top and works it’s way down. In practice, Lua first compiles your code into bytecode before executing it, but the compiler won’t balk at something like this:
Because the value contained in
greetis determined at runtime. It’s only when you actually try to call (i.e. invoke like a function) the value ingreet, at runtime, that Lua will discover that it doesn’t contain a callable value (a function or a table/userdata with a metatable containing a__callmember) and you’ll get a runtime error: “attempt to call global ‘greet’ (a nil value)”. Where “nil value” is whatever valuegreetcontained at the time the call was attempted. In our case, it wasnil.So you will have to make sure that that the code that creates a function and assigns it to
foois called before you attempt to callfoo.It might help if you recognize that this:
Is syntax sugar for this:
foois being assigned a function value. That has to happen before you attempt to call that function.The most common solution to this problem is to treat the file’s function as the “compilation time”, that is: declare all of your constant data and functions when the file is executed, ready to be used during “execution time”. Then, call a
mainfunction to begin the “execution time”.For example:
As
greethas been declared in_G,maincan access it.