lua-users home
lua-l archive

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


On Mon, Sep 14, 2009 at 10:24 PM, David Manura <dm.lua@math2.org> wrote:
> I'm surprised I haven't thought of this sooner (nor seen this posted
> elsewhere).  Here are two "module" function options that in a simple
> way avoid the problem of a module's private namespace being polluted
> to its public namespace.

That modules go into a global package table doesn't bother me, maybe
I'm just used to it... even languages like Java/C++/Ruby
that support namespacing don't allow completely private namespaces,
everything does have a one, true (possibly very long!) global
name.

There are lots of cool things that can be done with the module system,
though in the spirit of
Lua, you have to build it yourself.

Me, I want my modules to see the names in their enclosing modules, so I do this:

function package.seepackage(m)
    -- print("seepackage for", m)
    local mt = getmetatable(m) or {}
    local package = m._PACKAGE:sub(1, -2) -- strip trailing "."
    mt.__index = require(package)
    setmetatable(m, mt)
    return m
end

-- foo/something.lua
module(..., package.seepackage)

function do_something()
  some_support()
end

-- foo/support.lua
module(..., package.seepackage)

function some_support() end
function _kindof_internal() end

-- foo.lua
module(..., package.seeall)
require(_NAME..".support")

-- import support.* to top-level package

for k,v in pairs(support) do
    if type(k) == "string" and not k:match"^_" then
        _M[k] = v
    end
end

require(_NAME.."something")
--

require"foo"

foo.some_support()
foo.something.do_something()


Cheers,
Sam