# Mergewith() does not preserve alternate AbstractDict types. Bug?

**URL:** <https://discourse.julialang.org/t/mergewith-does-not-preserve-alternate-abstractdict-types-bug/122576>\
**Category:** General Usage\
**Tags:** potential-bug\
**Created:** [November 13, 2024, 9:32am UTC](https://discourse.julialang.org/t/mergewith-does-not-preserve-alternate-abstractdict-types-bug/122576 "2024-11-13T09:32:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)\
**Post date:** [November 13, 2024, 9:32am UTC](https://discourse.julialang.org/t/mergewith-does-not-preserve-alternate-abstractdict-types-bug/122576/1 "2024-11-13T09:32:27Z")

</div>

If I have two dictionaries of some alternative type like `OrderedDict`

```julia
julia> using OrderedCollections
julia> a = OrderedDict("a"=>1, "b"=>"foo")
OrderedDict{String, Any} with 2 entries:
  "a" => 1
  "b" => "foo"

julia> b = OrderedDict("a"=>1, "c"=>"bar")
OrderedDict{String, Any} with 2 entries:
  "a" => 1
  "c" => "bar"

```

then `merge()` will give me a result of the same type

```julia
julia> merge(a,b)
OrderedDict{String, Any} with 3 entries:
  "a" => 1
  "b" => "foo"
  "c" => "bar"

```

However, if I use `mergewith()` the result is always a `Dict`

```julia
julia> mergewith(*, a, b)
Dict{String, Any} with 3 entries:
  "c" => "bar"
  "b" => "foo"
  "a" => 1

```

This seems inconsistent. I wanted and expected the result to be an `OrderedDict`. Is this a bug?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 13, 2024, 11:58am UTC](https://discourse.julialang.org/t/mergewith-does-not-preserve-alternate-abstractdict-types-bug/122576/2 "2024-11-13T11:58:28Z")

</div>

At least it’s an annoying misfeature. `merge` returns an `OrderedDict` because `OrderedCollections` has a specialized method. Presumably it’s missing for `mergewith` because nobody updated `OrderedCollections` after `mergewith` was sort of split out from `merge`, cf. [mergewith not working properly for OrderedDict · Issue #77 · JuliaCollections/OrderedCollections.jl · GitHub](https://github.com/JuliaCollections/OrderedCollections.jl/issues/77).

A PR to fix this would probably be received favorably.

In the mean time a possible workaround is `mergewith!(*, copy(a), b)`, assuming that the key and value types of `a` are sufficient for the merged result. Or you can skip the `copy` if it’s fine to mutate `a`.

---

<div class="post-metadata">

**Author:** ![johnomotani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnomotani/32/26753_2.png) [@johnomotani](https://discourse.julialang.org/u/johnomotani)\
**Post date:** [November 13, 2024, 2:28pm UTC](https://discourse.julialang.org/t/mergewith-does-not-preserve-alternate-abstractdict-types-bug/122576/3 "2024-11-13T14:28:52Z")

</div>

PR opened [Implement mergewith() for OrderedDict and LittleDict by johnomotani · Pull Request #119 · JuliaCollections/OrderedCollections.jl · GitHub](https://github.com/JuliaCollections/OrderedCollections.jl/pull/119).
