lua-users home
lua-l archive

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


	Hi Thijs

In Lua I would simply protect a set with something like this;

function protect(self)
 return setmetatable(self, {
   __index = function(self, key)
     return rawget(self,key) or error("Key '"..tostring(key).."' not found.",2)
   end})
end

local set = protect({ a = 1 , b = 2 })

print(set.a)
print(set.b)
print(set.c)  --> error
	I wouldn't call this a "protection", but anyway...

I have a c module, for which I want to protect the module table itself like this (so a reusable function is not required, just a one-off). What is the recommended way of doing that? Probably someone did this before, but my google-foo failed to deliver. So any pointers to/or examples would be appreciated.
	Module tables are just like any other tables, so why can't you
do the same you showed up here?  Or maybe I've missed something.

	Regards,
		Tomás