Why is d::Dict !== Dict(d)?


#1

Does Dict(::Dict) copy only because the method is the vehicle for implementing copy(d::Dict)?

Fallback for T(::T):

(::Type{T})(arg) where {T} = convert(T, arg)::T # Hidden from the REPL.

usually leads to no copying:

julia> x = "a"; x === typeof(x)(x)
true

julia> x = (1,"a"); x === typeof(x)(x)
true

julia> x = Ref("a"); x === Ref(x)
true

julia> x = ["a"]; x === typeof(x)(x)
true

julia> x = Dict(1 => "a"); x === typeof(x)(x)
false

I’m having trouble drawing a distinction.

Dict(d::Dict) behavior originally implemented here.


#2

Looking at the code, what’s particularly weird is that the input dict is rehashed in place, and a copy of it is only taken after that. So copying isn’t even justified by the goal of not mutating the input dict


#3

Just having Dict(::Dict) copy seems weird to me. I opened an issue to track this: https://github.com/JuliaLang/julia/issues/23107