Recursive functions in LuaEdit
Self-recursion
Use this form:
local function e()
-- code that calls e()
end
Not this one, which triggers a "Need check nil" diagnostic:
local e = nil
e = function()
-- code that calls e()
end
Nor this one, which triggers an "Undefined global" diagnostic:
local e = function()
-- code that calls e()
end
Mutual recursion
None of the above forms work well for mutual recursion. The only one that avoids both "Need check nil" and "Undefined global" is to use a forward declaration that does not assign nil
. That is, this will work:
local a
local b
a = function()
-- code that calls b()
end
b = function()
-- code that calls a()
end
Note that the following alternative would be desirable, due to consistency with the recommended pattern for self-recursive functions, but it has problems of its own; namely, it leads to a "Redefined local" diagnostic:
local a
local b
local function a()
-- code that calls b()
end
local function b()
-- code that calls a()
end
References
Commits where I figured this out: