I found a strange MethodError
in my code. It is possible to reproduce it with the following simple code
struct A{T} x::T end
fun(a::A{T}, b::T) where {T} = "same parametric type"
fun(a::T, b::S) where {T<:A,S} = "any parametric type"
fun(b::S, a::T) where {T<:A,S} = "any parametric type"
Then, fun(A(A(1)), A(1))
returns an ambiguity where I expected it to return the string "same parametric type"
.
Does someone know if this is the expected behaviour? From my (certainly limited) understanding of the multiple dispatch, I do not see why this is ambiguous… (btw I use Julia 1.4.1)
EDIT: it seems that
f1(a::A{T}, b::T) where {T} = "same parametric type"
f1(a::T, b::S) where {T<:A,S} = "any parametric type"
does not yield methods ambiguity. However,
f2(a::A{T}, b::T) where {T} = "same parametric type"
f2(b::S, a::T) where {T<:A,S} = "any parametric type"
is enough to throw the MethodError
ambiguity. Moreover, the Possible fix
suggested by Julia is
Possible fix, define f2(::S, ::T) where {T<:A, S<:(A{T} where T)}
which to me seems superfluous.