[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: When does __index metamethod get called?
- From: "Paul Chiusano" <pchiusano@...>
- Date: Thu, 16 Mar 2006 20:00:51 -0500
> This also breaks iteration of the table. So these tables would need to be special cases, not good. Ugh, I think I'll just add a __setindex metamethod so everything can work like normal.
Another approach which you might not like :-) is to create a object
for representing these tables, and give them methods for getting and
setting indices ( a:get(1), a:set('a',b) ). Then you could install
whatever logic you want in the get and set methods, override or extend
this logic later, and so on. If you are really concerned about the
extra table lookup, you could even store the methods as locals, so
t[k] would become get(t,k) and t[k] = v would become set(t,k,v).
If you are not tied to the table[key] syntax, you can have whatever
semantics you want!