lua-users home
lua-l archive

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


I have a situation where it’s useful to store additional information associated with a key in a table.

 

I’m using an auxiliary table to store this additional information, using the same key as in the main table.

 

__newindex works fine for setting the entry in the auxiliary table when a key is added to the main one.

 

However, I’d like to remove the information from the auxiliary table when the key is removed (its value set to nil) from the main table, but there doesn’t seem to be a metamethod for this. Would it be a useful addition?

 

I want to remove it immediately (so it’s not a garbage collection issue that could be solved by weak tables. I guess I could use weak tables and force a GC, but that’s pretty ugly).

 

I could use a proxy table and catch every access via __newindex, but I think the problem with that is the main table then doesn’t behave right for things like pairs, because it’s empty.

 

Or is there some other way to do this?

 

P.