lua-users home
lua-l archive

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


on 3/8/07 5:08 AM, Roberto Ierusalimschy at roberto@inf.puc-rio.br wrote:

> If you want the gory details, there is another relevant stuff in the
> interaction between weak tables and userdata that is missing from the
> manual. Assume a userdata U that has a finalizer (a __gc metamethod).
> If U appears as a value in a weak(value) table, it is removed before
> the calling of its finalizer. But if U appears as a key in a weak(key)
> table, it will be removed only after the calling of its finalizer.
> (This allows the finalizer to access properties related to the userdata
> via a weak table.)

This also means that if you are trying to keep a weak set of values that
might include userdata values and you may iterate the set -- e.g. a set of
observers to be notified of some event -- you want to put entries into the
set as set[ obj ] = obj as opposed to set[ obj ] = true. (Or if false can
also show up in the set, then set[ obj ] = obj or true.)

Yes. I got bitten by this once.

Mark