[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: modules
- From: Paul Swanson <pswanson@<a href="/cgi-bin/echo.cgi?paulgames.com">...</a>>
- Date: Fri, 22 Feb 2002 12:04:23 -0800 (PST)
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
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 a.b.c.select() and
d.e.f.select(), 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 a.b.c.select(), and f.select() to
refer to d.e.f.select().
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,