local t = {}
local mt = setmetatable({
-- some meta method to know when a key is added or lost and prints a message
}, t)
Is there a way of doing this. I talked about this with someone and they said i couldn't just do it with meta methods but proxies as well. I'm a bit stumped on how to make this work. Can anyone help?
Thanks
To track table keys in lua there are 2 most importaint keys in metatable:
__indexand__newindex.__newindexis used to create new key in the table if such key is not found.__indexis used to get the value if there is no such key in table.With
__newindexit is possible to track creation, but not assignment, so it is not possible to track key removal:Using proxy table and
__newindexwith__indexwe can track every assignment:If you want enumerate table keys with
pairs(),ipairs(), then you need to use metakeys__pairsand__ipairsas original table is allways empty.