I have two functions that occasionally call each other, and they are local to a module that uses them to build another function. The module is somewhat similar to this piece of code:
local function iseven(n)
if n == 1 then
return false
else
return isodd(n - 1)
end
end
local function isodd(n)
if n == 1 then
return true
else
return iseven(n - 1)
end
end
local evenOrOdd = function(n)
return iseven(n) and "Even" or "Odd"
end
return evenOrOdd
The problem is, when calling evenOrOdd from another file I get the error attempt to call global 'isodd' (a nil value).
I noticed that this doesn't happen if I set iseven to global, but I would like to keep them local to my module.
I even tried putting a dummy function declaration (local function isodd() end) before the line where iseven is declared. I also tried inserting just local isodd in the place of the dummy function declaration, but in both ways it doesn't work and I get different kind of errors.
I know this is because Lua has closures and when iseven is declared it catches the actual value of isodd, which is either nil or the dummy function, and modifying it after doesn't count, but is there a way to bypass this?
local f; local function g() return f() end; function f() return g() endlocalbeforefunction isodd(n)and I got theattempt to call upvalue 'isodd' (a nil value)error. Thanks.attempt to call global 'isodd' (a nil value), which should explain what happened.function ()syntax is shorthand.) Again, functions are not declared.