# Multiple Dispatch with Unions, resolving ambiguities

**URL:** <https://discourse.julialang.org/t/multiple-dispatch-with-unions-resolving-ambiguities/46153>\
**Category:** Internals & Design\
**Created:** [September 6, 2020, 6:14pm UTC](https://discourse.julialang.org/t/multiple-dispatch-with-unions-resolving-ambiguities/46153 "2020-09-06T18:14:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [September 6, 2020, 6:14pm UTC](https://discourse.julialang.org/t/multiple-dispatch-with-unions-resolving-ambiguities/46153/1 "2020-09-06T18:14:46Z")

</div>

Let’s say we have these methods:

```julia
struct Type1 end
struct Type2 end

foo(a::Type1, i) = 1
foo(a::Type2, i) = 2
foo(a::Type1, i::Tuple{}) = 3
foo(b::Type2, i::Tuple{}) = 4

```

Here, everything dispatches as you would expect. Now let’s say you know that the implementation of the last two methods are identical, you’d probably do this:

```julia
bar(a::Type1, i) = 1
bar(a::Type2, i) = 2
bar(a::Union{Type1, Type2}, i::Tuple{}) = 3

```

But now there’s a MethodError when you try `bar(Type1(), ())`, saying this is ambiguous.

Shouldn’t a method with a Union parameter act like it was a separate method for each combination of types in the union? Or seen differently, what limitations exist that would make that difficult to do?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 6, 2020, 6:18pm UTC](https://discourse.julialang.org/t/multiple-dispatch-with-unions-resolving-ambiguities/46153/2 "2020-09-06T18:18:50Z")

</div>

This is related: [https://github.com/JuliaLang/julia/issues/22404](https://github.com/JuliaLang/julia/issues/22404)
