# Dict merge without promoting

**URL:** https://discourse.julialang.org/t/dict-merge-without-promoting/26648
**Category:** General Usage
**Tags:** type, dictionary
**Created:** [July 22, 2019, 8:23pm UTC](https://discourse.julialang.org/t/dict-merge-without-promoting/26648 "2019-07-22T20:23:34Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![grahamas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamas/32/3401_2.png) [@grahamas](https://discourse.julialang.org/u/grahamas)
#### Post date: [July 22, 2019, 8:23pm UTC](https://discourse.julialang.org/t/dict-merge-without-promoting/26648/1 "2019-07-22T20:23:34Z")

</div>

Is there any existing way to merge without promotion? Maybe with constructing a dict that has union type? Right now I’m trying to merge dicts that may have values of Int or Float type, in an arbitrary order, and I need the values to retain their type. Is there a way to do this aside from simply writing my own method?

(also, is there a particular reason why merge works with promotion? For me at least it led to an error that was hard to track down, and seems somewhat surprising)

---

<div class="post-metadata">

### Author: ![sswatson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sswatson/32/5135_2.png) [@sswatson](https://discourse.julialang.org/u/sswatson)
#### Post date: [July 22, 2019, 8:48pm UTC](https://discourse.julialang.org/t/dict-merge-without-promoting/26648/2 "2019-07-22T20:48:06Z")

</div>

Yes, you can use a union type or just `Any`:

```julia
D = Dict{Int,Any}()
D[1] = 2
D[2] = 3.0

```

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [July 22, 2019, 9:08pm UTC](https://discourse.julialang.org/t/dict-merge-without-promoting/26648/3 "2019-07-22T21:08:06Z")

</div>

If you need the key not to promote, you’ll need to use your own wrapper as far as I know. The key compares by `hash`, which per the docs says that

```julia
?hash

Compute an integer hash code such that isequal(x,y) implies hash(x)==hash(y). The optional second argument h is a
  hash code to be mixed with the result.

```

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [July 22, 2019, 10:17pm UTC](https://discourse.julialang.org/t/dict-merge-without-promoting/26648/4 "2019-07-22T22:17:01Z")

</div>

You could create a `Dict` of whatever type you want and use `merge!`,

```julia
julia> d1 = Dict(1 => 2); d2 = Dict(2. => 3.); d3 = Dict{Union{Int, Float64}, Union{Int, Float64}}();

julia> merge!(d3, d1, d2)
Dict{Union{Float64, Int64},Union{Float64, Int64}} with 2 entries:
  2.0 => 3.0
  1 => 2

```

---

<div class="post-metadata">

### Author: ![Daniel\_Berge](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@Daniel\_Berge](https://discourse.julialang.org/u/Daniel_Berge)
#### Post date: [July 22, 2019, 11:52pm UTC](https://discourse.julialang.org/t/dict-merge-without-promoting/26648/5 "2019-07-22T23:52:24Z")

</div>

I misunderstood what was being asked, as I was thinking the question pertained to keys being promoted. So if it’s expected that `d[1.0] != d[1]`, you’d need a wrapper type.
