lua-users home
lua-l archive

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

> For userdata being finalized in weak tables, why keep them in keys,
> but not in values?
> Are there special considerations?

Yes. Frequently we associate attributes to an object through a weak table:

  T[object] = object_attribute

This object may be a userdata, and this userdata may need this attribute
during its finalization. If Lua removes this entry from the table
(because 'object' is no longer accessible), the __gc metamethod will not
be able to access that data.

On the other hand, as the object is no longer accessible, it does not
make sense to keep it as a value in a table, because Lua should not
have "common" access to it anymore.

(Note that, except for traversals, when the object is a key you need a
previous access to it to access the entry. This is not true when the
object is a value.)

-- Roberto