lua-users home
lua-l archive

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

On 3/16/06, David Morris-Oliveros <> wrote:
Now, to track both insertions and overwriting of existing entries in the
table, you have to use a proxy table (or something like it). Example:

tbl = {}
tbl.__proxy = {}
mt = {__index = function(t,k) return t.__proxy[k]; end,
      __newindex = function(t,k,v) print('tracked', k, v); t.__proxy[k]
= v; end}
setmetatable(tbl, mt)

This works by making all values "stored" in tbl not actually stored
there, so when there's an access to it (get), it'll invoke the __index
metamethod, and retrieve it from __proxy. And when there's a setting of
it, since it doesn't exist in tbl, but in tbl.__proxy, it'll go through
your __newindex metamethod.

Thanks, that's what I was looking for.   Wow, a lot of overhead there because you're basically doubling all lookups in the table (well it's worse than that because it's two lookups in two different tables).  Doesn't seem that great, seems like there should be a __setindex metamethod or something.  Or possibly call __index also when setting not just reading.  Although I'm not sure how that would work.

// Chris