Namespaces And Modules |
|
There is a Lua technical note which dicusses modules and packages [1].
dofile command could be expanded to dofile(filename, table) and the table passed used as the global table for executing the file? e.g.,
String = {}
dofile("stringlib.lua",String)
x = String.strlen("a string")
function dofile(f,t) local globals=globals local g=globals(t) local rc=%dofile(f) globals(g) return rc end
(lhf) If you're willing to use a "global" declaration, then something very similar can be done right now: Instead of a "global" keyword, use a "global" function, with the syntax:
global "myvar" -- one name only
global{"a","b"} -- several names
do
local G={}
function global(x)
if type(x)=="string" then
%G[x]=1
else
for i,v in x do
%G[v]=1
end
end
end
end
You can now implement the semantics you want by setting tag methods for "setglobal" and/or "getglobal" that checks the name against G.
Thus to implement the need-declaration-to-write-to-globals semantics, set the "setglobal" tag method and raise an error if the given variable is not in G. The error will be show as soon as it happens; ie no sleeper!
For details, see the FAQ on uninitialized and read-only variables.
The problem with this approach is that declarations last for ever, not only for the current block.
(ET) Or the simple way:
global = globals() ... global.x = 1 print(global.y)