Hello,
I'm trying to write a finalizer for an object that is also used as the key
in a WeakKeyDict{k,v}, and I'm having a bit of trouble getting it to work.
The context is that I need to use the WeakKeyDict to handle a bunch of
additional cleanup before the WeakKeyDict's deleter handles the removal of
the key. So I have to run my program-specific finalizer before the
WeakKeyDict's finalizer.
A couple questions:
1. Are finalizers "stackable" on the same object? if not, should it? if
they are, is the order LIFO (the last function fed into finalizer(x,f) gets
called first), FIFO or random (no guarantee)?
2. There's another approach which may avoid answering #1 altogether,
which is to add a deleter_wrapper field (::Function, default identity) to
WeakKeyDict during set up, like so:
function add_weak_key(t::Dict, k, v)
if is(t.deleter, identity)
if t.delete_wrapper != identity
t.deleter = x->(t.deleter_wrapper(x); weak_key_delete!(t,x))
else
t.deleter = x->weak_key_delete!(t, x)
end
end
t[WeakRef(k)] = v
# TODO: it might be better to avoid the finalizer, allow
# wiped WeakRefs to remain in the table, and delete them as
# they are discovered by getindex and setindex!.
finalizer(k, t.deleter)
return t
end
Thanks for your consideration.
T