You've got it right on the formal parameter side: Use the function call operator ()
on the expression func
. If the value of the expression is actually a function, it'll be called; If not, Lua will throw an error.
On the actual argument side, you have a mistake. To pass an argument, simply put an expression in the argument list. You want to pass the value in the variable bla
so execfunc(bla)
. The fact that the expression is simply a variable and the value in the variable is a function value doesn't change anything.
Lua variables are dynamically typed. The compiler doesn't keep track of which variables would be last assigned function values before they are used. In fact, it can't. You could write a function that sometimes returns a function and sometimes returns a number. In either case, you can still try to call the result.
local f = function ()
if Wednesday then
return 7
else
return function (n) return n + 1 end
end
end
local g = f()
g() -- is the value in g a function or not? It doesn't matter to the compiler.