lua-users home
lua-l archive

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


On Thursday 16 March 2006 6:20 pm, Chris wrote:
> Example:
>
>    tbl = {}
>    setmetatable(tbl, {__index = function(t,k) print("test") end})
>    tbl[1] = 1
>    tbl[2] = 2
>
> Prints nothing... ??

as Jerome said: __index is the getter, you need __newindex there.

> Also, is there any metamethod that you can hook to know when a value is
> being set?  I know there is __newindex but that only appears to be called
> for new values being inserted into the table.  I want to hook both cases of
> new values and also setting existing values.

if __newindex doesn't set the value in the table, it'll be called each time, 
because it'll always be a new index.  you can store values somewhere else and 
modify __index to fetch them there.  (i think that's called a proxy table)


-- 
Javier

Attachment: pgpnT71ebPlCB.pgp
Description: PGP signature