[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: protect a set in c (non-existing elements)
- From: Thijs Schreijer <thijs@...>
- Date: Fri, 14 Feb 2014 13:46:17 +0000
> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Tomas Guisasola Gorham
> Sent: vrijdag 14 februari 2014 12:04
> To: Lua mailing list
> Subject: Re: protect a set in c (non-existing elements)
>
> 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
Obviously you're right. But this is simple in Lua and a bit cumbersome in c. So my question was more on how others solved it; possibly including a lua file in the module that does it, or embedding lua code in c (tried to find an example, but couldn't find it)
What's the most elegant way of doing it?
Thijs