[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: modules
- From: Roberto Ierusalimschy <rieru@<a href="/cgi-bin/echo.cgi?delirius.cs.uiuc.edu">...</a>>
- Date: Fri, 22 Feb 2002 15:39:26 -0600 (CST)
On Fri, 22 Feb 2002, Paul Swanson wrote:
> 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.
If I understand you correctly, we do not need 'global' for that. You can
always refer to a module function by its fully qualified name. The 'global'
is to used exactly when we do not want to use a fully qualified name (or
more exactly, any qualified name).
> 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.
Such manipulations of tables (and other "modules") was what I meant
when I said that we can do a lot of module functionality in Lua itself.
But, when you manipulate the global table, you (obviously) have global
consequences. For instance, once you import a module somewhere, it is
imported everywhere. If two completely unrelated parts of your program
include two unrelated modules with conflicting names, you will get a
"duplicate" error. One of the goals of the "global" declaration is to
reduce this "heavy" use of the global table.