[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: compile-time globals
- From: "Patrick Donnelly" <batrick.donnelly@...>
- Date: Tue, 21 Aug 2007 00:55:32 -0600
Forgive me if this has been done before, but why isn't this construct
sufficient?
real = { };
setmetatable(real, {__index = _G});
local sub = {};
setmetatable(sub, {__index = real, __newindex = function(tab, key, value)
if not tab[key] then
error("cannot create global");
else
real[key] = value;
end;
end});
setfenv(1, sub);
real.hi = 5; -- create a global
print(sub.hi); --> 5
hi = "hello"; --changing an existing global is ok
print(real.hi); --> hello
k = 6; --> stdin:1: cannot create global
--
-Patrick Donnelly
"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."
-Will Durant