lua-users home
lua-l archive

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

I liked what Roberto had to say about using:

global (ModuleTable) var1, var2;

to redirect global references to var1 and var2 to ModuleTable.  It seems
like a very flexible yet simple way to accomplish a lot of different
goals.  Maybe, though, there is a way to generalize it even further:

redirect globals() to ModuleTable for var1, var2;

(specific grammar is only mentioned for clarity).

Then you could also say:

redirect myTable to myOtherTable for var1, var2;

which in effect maps myTable.var1 to myOtherTable.var1, and var2 also.

On another note, I was going to suggest:

-- imports all of a module into the current global namespace
function import(modulename)
   local t = loadLibrary(modulename)
   local g = globals()
   mergeTablesWithErrorOnDuplicate(g, t) -- causes an error if an index in
                                         --  t already exists in g

This is not as general as Roberto's idea, but has the added bonus of being
completely implementable in the current Lua 4.1.  Here's a possible
gotcha, though:  setting the globals() table removes all previously
imported modules.   Maybe this is a good thing.

In response to the unasked question "How do I use two functions with the
same name in two different modules?"  Given and, you would do:

import("a.b.c")  -- assuming you wanted the first one to be the default

then you would use select() to refer to, and to
refer to

It would be nice to be able to refer to a module function by its fully
qualified name, as in C++ (e.g. std::string).  I don't know if this is
possible with the 'global (m) var;' idea, though, either.

Thanks for listening,