Weak key IdDict? (Or other “metadata” solutions?)

I’d like for one piece of my code to be able to associate data with individual objects in another part – metadata or the like, that’s not part of the original type definition. I could write wrappers/decorators, but I’d like to be able to look up the values using the original objects. I could do this with an IdDict, but that’d interfere with GC; and a WeakKeyDict wouldn’t discern between non-identical but equal objects (in most cases). Yet there is no combination in Base, as far as I can see. Is this because it somehow doesn’t make sense, or just that it hasn’t been written? (I could write one myself, but I’d be happy to hear other ideas for doing this kind of thing.)

What’s wrong with a Dict()?

Given your question, perhaps nothing?-)

I was assuming that it (1) doesn’t treat its keys as weak references, and (2) doesn’t discern between equal (but non-identical) objects – the two properties I’m trying to combine. Am I incorrect about that? Or were you thinking something more elaborate than using the Dict directly?

The reasoning behind the properties are that (1) I’m trying to represent properties of the objects, and the existence of these properties should not prevent the objects from being gc-ed, and (2) mutable objects that are temporarily equal in other regards should not suddenly aquire the same properties (in the sense of this mapping).

Ok, I didn’t realize from your text that that was an explicit requirement.

Ah. Good to get that clarified, then! :slight_smile:

There is none. Although Stefan reckons that the WeakKeyDict should be an ID dict: https://github.com/JuliaLang/julia/issues/24941.

1 Like

Ah, yes, that does make sense.

Now as a PR in DataStructures.jl: https://github.com/JuliaCollections/DataStructures.jl/pull/402

2 Likes