# Merging dictionaries indexed by elements of struct type

**URL:** https://discourse.julialang.org/t/merging-dictionaries-indexed-by-elements-of-struct-type/30499
**Category:** General Usage
**Tags:** dictionary, data\_structures
**Created:** [October 30, 2019, 3:32pm UTC](https://discourse.julialang.org/t/merging-dictionaries-indexed-by-elements-of-struct-type/30499 "2019-10-30T15:32:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Pierre-Emmanuel\_Chap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-emmanuel_chap/32/11068_2.png) [@Pierre-Emmanuel\_Chap](https://discourse.julialang.org/u/Pierre-Emmanuel_Chap)
#### Post date: [October 30, 2019, 3:32pm UTC](https://discourse.julialang.org/t/merging-dictionaries-indexed-by-elements-of-struct-type/30499/1 "2019-10-30T15:32:12Z")

</div>

Hello, I am failing merging correctly dictionaries that are indexed by a mutable struct that I defined before. Here is the code of a baby example :

mutable struct my\_vector  
v::Vector{Int}  
end

Base.:(==)(v1::my\_vector,v2::my\_vector) = (v1.v==v2.v)  
Base.isequal(v1::my\_vector,v2::my\_vector) = (v1.v==v2.v)

v1=my\_vector([1,2])  
v2=my\_vector([1,2])

d1=Dict([(v1,1)])  
d2=Dict([(v2,1)])  
println(merge(+,d1,d2)) # Dict(my\_vector([1, 2])=\>1,my\_vector([1, 2])=\>1)

D1=Dict([([1,2],1)])  
D2=Dict([([1,2],1)])  
println(merge(+,D1,D2)) # Dict([1, 2]=\>2)

I would like to know if it is possible to correct the code so that the output on the first line would be  
Dict(my\_vector([1, 2])=\>2)

Thanks for your help ! 😉

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [October 30, 2019, 3:57pm UTC](https://discourse.julialang.org/t/merging-dictionaries-indexed-by-elements-of-struct-type/30499/2 "2019-10-30T15:57:52Z")

</div>

It looks like you’re missing a definition of `Base.hash` for your type. From the documentation for `isequal` (which you can read by typing `?isequal`):

```julia
isequal is the comparison function used by hash tables (Dict). isequal(x,y) must imply that hash(x) == hash(y).

```

Since your `isequal` and `==` tests just check the `v` member, you can do the same for hashing:

```julia
julia> Base.hash(v::my_vector, seed::UInt) = hash(v.v, seed)

```

Also, please quote your code so that it is easier to read. You can find some helpful instructions on how to do that here: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

### Author: ![Pierre-Emmanuel\_Chap](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-emmanuel_chap/32/11068_2.png) [@Pierre-Emmanuel\_Chap](https://discourse.julialang.org/u/Pierre-Emmanuel_Chap)
#### Post date: [October 30, 2019, 6:19pm UTC](https://discourse.julialang.org/t/merging-dictionaries-indexed-by-elements-of-struct-type/30499/3 "2019-10-30T18:19:12Z")

</div>

Thanks ! This is exactly what I needed.
