Is there a recommended way to getting around that? The example above had a union of only two types, but in the actual code I'm working on there are a couple more. Would I have to copying the code over and over with just small changes to the type signature? I guess you could use a macro to splice the types in.
On Friday, July 8, 2016 at 7:58:02 PM UTC-7, Yichao Yu wrote: > > On Fri, Jul 8, 2016 at 10:32 PM, Darwin Darakananda > <[email protected] <javascript:>> 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 >
