From c9d8d219d6fa064b7b056ac3e3244bdb2c50be6a Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Sun, 15 Mar 2026 12:33:10 +0100 Subject: [PATCH] Support different subtypes of AbstractDict in merge()/mergewith() The merge functions internally use the _typeddict() helper function, which returns a new Dict with the correct promoted key/value types. But the fact that it always returns a Dict means that the default implementations of merge()/mergewith() cannot be used for new AbstractDict subtypes that wish those functions to return their own custom type. --- base/abstractdict.jl | 4 ++-- test/dict.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/base/abstractdict.jl b/base/abstractdict.jl index cdea331d49c51..d36c2dbf83eec 100644 --- a/base/abstractdict.jl +++ b/base/abstractdict.jl @@ -406,10 +406,10 @@ promoteK(K) = K promoteV(V) = V promoteK(K, d, ds...) = promoteK(promote_type(K, keytype(d)), ds...) promoteV(V, d, ds...) = promoteV(promote_type(V, valtype(d)), ds...) -function _typeddict(d::AbstractDict, others::AbstractDict...) +function _typeddict(d::T, others::AbstractDict...) where {T <: AbstractDict} K = promoteK(keytype(d), others...) V = promoteV(valtype(d), others...) - Dict{K,V}(d) + T.name.wrapper{K, V}(d) end """ diff --git a/test/dict.jl b/test/dict.jl index a3f3ed0de5305..03e403f27a8c0 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -1349,8 +1349,8 @@ end @test d == IdDict(1 => 6, 2 => 5, 3 => 10) @inferred mergewith(+, d1, d2, d3) d = mergewith(+, d1, d2, d3) - @test d isa Dict{Int, Float64} - @test d == Dict(1 => 6, 2 => 5, 3 => 10) + @test d isa IdDict{Int, Float64} + @test d == IdDict(1 => 6, 2 => 5, 3 => 10) end @testset "misc error/io" begin