Hi, does anyone know in the code below, why #2 has convert called and how to avoid it?
struct S{T}
i::T
end
mutable struct Box{T}
i::T
end
function Base.convert(::Type{S{T1}}, d::S{T2}) where {T1, T2}
println("Convert: ", T2, " --> ", T1)
return S{T1}(zero(T1))
end
function Base.copy(box::Box{T}) where {T}
println("Different Types? --> ", T, ", ", typeof(box.i))
Box{T}(box.i)
end
# 1.
b = Box{Any}(S{Float64}(1.0)) # no conversoion
c = copy(b) # no conversion
# 2.
b = Box(S{Float64}(1.0)) # Conversion
c = copy(b) # Conversion
Yes, there’s a default convert(::Type{T}, x::T) where {T} = x method builtin, but your convert method is more specific. You could similarly add a convert(::Type{S{T}}, x::S{T}) where {T} = x to avoid this case.
I guess I typically don’t see that convert call because of the no-op default.