Re: Mutual requires
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Mutual requires
- From: Kevin Martin <kev82@...>
- Date: 2012年12月20日 22:11:04 +0000
On 20 Dec 2012, at 19:59, Geoff Leyland wrote:
Have you considered using a lazy require, something like:
function lazyrequire(name)
local mt = {}
function mt.__index(self, key)
local m = require(name)
for k, v in pairs(m) do
self[k] = v
end
setmetatable(self, getmetatable(m))
return m[key]
end
local t = {}
setmetatable(t, mt)
return t
end
local dbg = lazyrequire("debug")
dbg.debug()
I know it's not perfect, as each require call site will have it's own separate copy of the module table, but I don't think that matters in most circumstances.
Thanks,
Kev