[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: lua hacking wondering
- From: Dimitris Papavasiliou <jimmyp@...>
- Date: Thu, 24 Jun 2004 02:46:52 +0300
Roberto Ierusalimschy wrote:
The point is not performance. Whenever you set a __gettable metamethod
to a table, you cannot access that table except through a "rawget" call.
In other words, you already need some special way to access the table.
That is not much different than the use of a proxy. More important, it
is quite easy to encapsulate the proxy "pattern" inside a function, so
that anyone that needs to track table accesses just call that function
(see PIL, page 117).
If people keep asking something, it does not imply that the language
needs that something built-in; it may mean only that it needs better
documentation (FAQ comes to mind :) or external libraries.
I agree on that, but the points I want to make are the following:
1) It seems to me more natural that the metamethods for a table should
be __settable and __gettable rather than __index and __newindex. The
main (rather the only I think) operations performed on a table (from a
logical point of view and not necessarily in the context of lua tables)
are setting and getting values not setting new values and reading
non-existing ones. Therefore these are the metamethods one would expect
(and in maybe the majority of cases need) and that explains the great
demand and nagging on the matter. In fact my first posts on this list
(when Lua5 was first released) were on this point and while I considered
then (and now) proxy tables as a cool trick, I still see them as a
workaround/hack for something that logically should not need a hack to
implement. Now tracking the getting and setting of non-existing values
in tables is useful and all but it can't (IMHO) be considered an
elementary operation on tables but rather a special case of get/set
operations and thus it is logical that they be implemented on top of
get/set operations and not the other way round as is now the case. All
this is from a theoretical, logical and mechanical point of view.
2) Now from a practical point of view, I have been using lua mostly as
an interface to my engine (a game engine I suppose although it could be
used for other things as well) so I don't have much experience with
writing lua code but rather with integrating lua and with the lua API.
In this context the absence of __get/__set methods tend to complicate
lua integration C code a bit. Say for example, I need to implement a
list of objects of some sort and it should be manipulatable from Lua.
I'll have to keep references to two tables, the proxy and the backend
table, and my C metamethods will have to deal with both of them. Ok, I
agree, it's nothing tragic and therefore I haven't been nagging much
lately but I'm still annoyed by the fact tha my code get's complicated
for no reason. I mean logically all I should need would be a table to
store the objects (as userdata or whatever) and a pair of __get/__set
methods to sync Lua/C state whenever the table is altered. There might
be a better way to handle things which would lead to better/simpler code
as you point out but that still wouldn't change anything from the
theoretical point of view.
Well that's all, sorry for the long post,