lua-users home
lua-l archive

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


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?

That depends on the level (or user experience) the description is aimed
at. A simple description of how tables work and how they can be used in
a script is one thing; the metamethods don't come into play here.

On a deeper level, they do of course; in this case more methods mean
more scope for confusion. But that's always been the price to be paid
for added complexity.

> > 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?

__usedindex (as implemented in Idle) works very similar to __newindex
but it's called if a field is already in the table. IOW, the return
value of __usedindex is not the relevant point here. The function called
by __usedindex (or __newindex) acts as a gatekeeper which decides
whether or not a certain assignment will go ahead (ie it either performs
it or it doesn't).

-- 
cheers  thomasl

web: http://thomaslauer.com/start