On Fri, Jul 8, 2016 at 10:32 PM, Darwin Darakananda
<[email protected]> wrote:
> 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?

Yes.

> I thought that (::Union{Int, MyType}, ::A)
> would be a more specific match to (::B, ::A) than (::MyType, ::MyType).

There's basically nothing as a "more specific match". The two methods
are ambiguous and anything in their intersection cannot be dispatched
to either of them.

>
> Any ideas/suggestions?
>
> Thanks,
>
> Darwin

Reply via email to