lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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