lua-users home
lua-l archive

Re: Private modules, was Calling module from a C function

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


On 2007年4月25日, Thomas Lauer wrote:
> roberto@inf.puc-rio.br (Roberto Ierusalimschy) wrote:
> > I don't think so. As far as I know, the "pollution" of the global
> > namespace by one name per module is quite theoretical. The problem
> > created by 'seeall' is trivially fixed by not using 'seeall'.
> 
> I like these simple solutions:-)
> 
> However, an official function in module package to *selectively* import
> stuff from other modules would be a welcome addition to the module
> system, IMHO. I know that's easy to write, but as usual everybody will
> have his or her own version and it all won't play nicely together.
	Have you wrote your own version? I think a copy function
is easy to write, but I'm not certain on how to handle function
dependencies when importing a function f from module A to module B.
What to do if f uses a global g and B already has a member g?
	For my purposes, I wrote a substitute for module:
function addtomodule (modname, requiredname)
 local _M = require(modname)
 if requiredname then
 package.loaded[requiredname] = _M
 end
 setfenv (2, _M)
end
	It uses an already defined module instead of creating a new
table. In fact, I don't like it. I would prefer to let the user
decide if he/she wants to open a module inside another one, instead
of the module's developer. I would like to write:
local table = require("table.extra", "table")
	The `table/extra.lua' file could be written:
local modname = select(2, ...) or select(1, ...)
module (modname)
	But `require' only passes its first argument to the script :-(
		Tomás

AltStyle によって変換されたページ (->オリジナル) /