lua-users home
lua-l archive

protect a set in c (non-existing elements)

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


Hi list,
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 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.
Thx!
Thijs

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