问题
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?
回答1:
The problem is that the call to isodd in iseven uses a global variable, not the local one defined later.
Use forward declarations, as suggested by @Egor:
local iseven, isodd
function iseven(n)
...
end
function isodd(n)
...
end
...
回答2:
Another way to get past this problem is to use tables. Plain local variables are probably more efficient, but with tables you dont need to manage the declarations.
local T = {}
local function evenOrOdd(n)
return T.iseven(n) and "Even" or "Odd"
end
function T.iseven(n)
-- code
end
The gist of this is that because the table is defined at the top, everything below it has access to it and you can dynamically change it's contents.
When evenOrOdd is called, T.iseven should already be defined by then even though it was not defined when evenOrOdd was defined.
回答3:
Better check for num%2 - remainder of the division
来源:https://stackoverflow.com/questions/38502549/local-functions-calling-each-other