[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: When does __index metamethod get called?
- From: Chris <coderight@...>
- Date: Thu, 16 Mar 2006 20:31:13 -0500
On 3/16/06, Paul Chiusano <firstname.lastname@example.org> wrote:
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!
Thanks, that might work although the whole reason I want things like
iteration to work is less for my sake and more for the people using my
API. As I programmer I know I hate it when there are
inconsistencies and special cases that you have to remember when you
shouldn't need to worry about that stuff. What I worry
about with this approach is that there is no way to prevent them from
using the normal syntax.