[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Mutual recursion
- From: Andrew Gierth <andrew@...>
- Date: Thu, 23 Apr 2020 23:20:44 +0100
>>>>> "Martin" == Martin <dyngeccetor8@disroot.org> writes:
Martin> I believe you can solve a lot of problems with hammer, duct
Martin> tape and Lua "debug" module.
Might be preferable to avoid the debug module in general; metatables
can do the job too (cqueues does something like this):
--[[ main.lua ]]
local m = require 'mod1'
print(m.foo(3))
--[[ mod1.lua ]]
local M = {}
local m2 = require 'mod2'
function M.foo(n)
print("in mod1.foo",n)
if n and n > 0 then
m2.bar(n - 1)
end
return "foo";
end
return M
--[[ mod2.lua ]]
-- Lazy loading to avoid mutual recursion issue
local function loader(M,k)
setmetatable(M,nil)
local m1 = require 'mod1'
function M.bar(n)
print("in mod2.bar",n)
return m1.foo(n)
end
return rawget(M,k)
end
return setmetatable({}, {__index = loader})
--
Andrew.
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org