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

**URL:** https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377
**Category:** General Usage
**Created:** [July 14, 2018, 4:03pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377 "2018-07-14T16:03:59Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)
#### Post date: [July 14, 2018, 4:03pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/1 "2018-07-14T16:03:59Z")

</div>

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.)

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 16, 2018, 1:24pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/2 "2018-07-16T13:24:39Z")

</div>

What’s wrong with a `Dict()`?

---

<div class="post-metadata">

### Author: ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)
#### Post date: [July 16, 2018, 1:41pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/3 "2018-07-16T13:41:02Z")

</div>

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).

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [July 16, 2018, 1:42pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/4 "2018-07-16T13:42:45Z")

</div>

> [@mlhetland](#):
>
> I was assuming that it (1) doesn’t treat its keys as weak references

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

---

<div class="post-metadata">

### Author: ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)
#### Post date: [July 16, 2018, 1:48pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/5 "2018-07-16T13:48:34Z")

</div>

Ah. Good to get that clarified, then! 🙂

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [July 17, 2018, 6:24am UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/6 "2018-07-17T06:24:32Z")

</div>

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

---

<div class="post-metadata">

### Author: ![mlhetland](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlhetland/32/283_2.png) [@mlhetland](https://discourse.julialang.org/u/mlhetland)
#### Post date: [July 17, 2018, 6:27am UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/7 "2018-07-17T06:27:20Z")

</div>

Ah, yes, that does make sense.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [July 20, 2018, 2:03pm UTC](https://discourse.julialang.org/t/weak-key-iddict-or-other-metadata-solutions/12377/8 "2018-07-20T14:03:48Z")

</div>

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