Hi everyone,

I have some code where multiple types share the same implementation of a 
method, for example:

abstract MyType


type A <: MyType end
type B <: MyType end


f(target::MyType, source::MyType) = "fallback"


f(target::Int,    source::A) = "from A"
f(target::MyType, source::A) = "from A"

a = A()
b = B()

f(b, b) # fallback
f(b, a) # from A
f(a, a) # from A

I was hoping that I could replace the "from A" function using a union type, 
but I'm running into ambiguity errors:

f(target::Union{Int, MyType}, source::A) = "from A"

f(b, b) # fallback
f(b, a) # Ambiguity error
f(a, a) # Ambiguity error

Is this an expected behavior?  I thought that (::Union{Int, MyType}, 
::A) would be a more specific match to (::B, ::A) than (::MyType, ::MyType).

Any ideas/suggestions?

Thanks,

Darwin

Reply via email to