[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: lua hacking wondering
- From: Mark Hamburg <mhamburg@...>
- Date: Thu, 17 Jun 2004 10:45:09 -0700
I modified Lua to support a __write metamethod which if present gets called
whether the index exists or not. The semantics relative to __newindex get
tricky to define but not because of the code. The code itself was quite
straightforward as I recall and made me feel good about how well thought out
the base Lua implementation was.
Now comes a report of an effort to create an __existingindex metamethod.
(From my experience, I would say start by tracing through the handling of
the existing __index metamethod.)
Always, the standard answer is to use proxy tables. These work, but are
somewhat expensive with respect to space and they break in other ways such
as with respect to table iteration. They also won't catch manipulation
through routines like table.insert. They are also clumsy to set up if the
table already exists and is populated.
Here is a quick summary of ideas (some mine, some from others) for improving
these fairly common design patterns for Lua. They have some overlap and it
might not be wise to do all of them. I post them here to encourage comments.
* Add table.swap( a, b ) which swaps the contents of two tables a and b.
This is useful when converting existing tables into proxies since it keeps
all of the old references working.
* Add a protected mode to tables that would force use of __index and
__newindex. (It might be nice to have the option to just do
* Add metamethods for the other standard table access (e.g., iteration) and
manipulation (table namespace) routines.
* Give tables a default metatable and shift from writing:
table.insert( t, v )
t:insert( v )
This can mostly be done now by setting table as the metatable for an empty
table, but it isn't done automatically and hence code is more likely to use
the first form. The second form, however, makes it easier to override the