# What's the expected behaviour when using vector as dict(hash) key?

**URL:** <https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431>\
**Category:** General Usage\
**Created:** [September 5, 2019, 2:53pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431 "2019-09-05T14:53:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![runapp](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@runapp](https://discourse.julialang.org/u/runapp)\
**Post date:** [September 5, 2019, 2:53pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431/1 "2019-09-05T14:53:46Z")

</div>

```julia
a,k=Dict(),[1]
a[k]=1
k[1]=2
print(a," ",a[k]," ");print(a[[1]]," ");println(a[[2]]);
a[k]=2
print(a," ",a[k]," ");print(a[[1]]," ");println(a[[2]]);

```

What should we expect? Or should we completely forbids using vectors(arrays) as key? Then why does hash for array relies on value rather than objectid?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 5, 2019, 2:57pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431/2 "2019-09-05T14:57:43Z")

</div>

Arrays compare equality by their contents, thus they hash by their (current) contents. If you change the contents of an array that’s being used as a dictionary key, you’ll no longer be able to find it in the dictionary. This is the expected behavior, and I think we have it documented… if not let’s add it as a warning to the documentation.

---

<div class="post-metadata">

**Author:** ![runapp](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@runapp](https://discourse.julialang.org/u/runapp)\
**Post date:** [September 5, 2019, 3:07pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431/3 "2019-09-05T15:07:36Z")

</div>

However when using `Dict` in callback functions, and of course we can’t require the caller to ensure it doesn’t reuse any arrays, disaster comes about. Is this still by design?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [September 5, 2019, 3:26pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431/4 "2019-09-05T15:26:49Z")

</div>

Assuming that the keys are immutable is pretty common, I think. You could submit a PR to Base that adds an

```julia
index = isimmutable(index) ? index : copy(index)

```

to `base/dict.jl` `setindex!` methods and see what people think. Or create a `MutableKeyDicts` package.

---

<div class="post-metadata">

**Author:** ![runapp](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@runapp](https://discourse.julialang.org/u/runapp)\
**Post date:** [September 5, 2019, 4:27pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431/5 "2019-09-05T16:27:54Z")

</div>

I agree. From personal perspect I’d prefer adding MutableKeyDicts, and forbids mutable keys in Dict. Hope no one has opened a similar issue.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 5, 2019, 5:15pm UTC](https://discourse.julialang.org/t/whats-the-expected-behaviour-when-using-vector-as-dict-hash-key/28431/6 "2019-09-05T17:15:08Z")

</div>

> [@runapp](#):
>
> Then why does hash for array relies on value rather than objectid?

If you want to hash based on object-id, then you can use the built-in `IdDict` type:

```julia
d = IdDict()
k = [23]
d[k] = "yes"
k2 = [23]
d[k2] # throws exception
get(d, k2, "no") # returns "no"
k[1] = 17
d[k] # returns "yes"

```

This is probably the behavior you want if you are using arrays as keys (and is also much faster than hashing the array contents).
