lua-users home
lua-l archive

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


On Wed, Nov 18, 2009 at 10:38 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> On Wed, Nov 18, 2009 at 12:21 PM, Thomas Lauer <thomas.lauer@virgin.net> wrote:
>> I've tried to "orthogonalise" the available metamethods. The beauty of
>> metamethods is that if you don't want or need them, they are not getting
>> in the way, so I reckoned a more rounded spectrum would be helpful for
>> some applications.
>
> That seems a most desirable goal. The balance here is 'explainabilty',
> e.g. does it significantly complicate any description of table lookup?
>
>> http://lua-users.org/wiki/LuaPowerPatches
>
> quote: "__usedindex behaves exactly like __newindex but when the
> indexed key actually exists (value is overwritten). this allows simple
> implementation of read-only tables, mirroring C structures etc without
> slow/lengthy/fragile table proxying constructs."
>
> So, if you want a read-only table, you make __usedindex always return nil?

You mean "throw an error", presumably? The return value of __usedindex
or __newindex isn't used. You'd also need to set __newindex to do the
same thing, otherwise people could add their own arbitrary fields
(that they then wouldn't be able to change).

> This would also be very useful for implementing properties...

You mean, the "raw" table entry for a property name would be a
getter/setter function, and __usedindex would call it? That works for
setting, but what about getting - you'd still only get the function
itself.

-Duncan