# Method ambiguity for unions

**URL:** https://discourse.julialang.org/t/method-ambiguity-for-unions/23086
**Category:** Internals & Design
**Tags:** question
**Created:** [April 12, 2019, 5:23pm UTC](https://discourse.julialang.org/t/method-ambiguity-for-unions/23086 "2019-04-12T17:23:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [April 12, 2019, 5:23pm UTC](https://discourse.julialang.org/t/method-ambiguity-for-unions/23086/1 "2019-04-12T17:23:37Z")

</div>

I have hit the following problem already several times:

```julia
ERROR: MethodError: f(::Int64, ::Int64) is ambiguous. Candidates:
  f(x::Union{Float64, Int64}, y::Int64) in Main at REPL[4]:1
  f(x::Int64, y) in Main at REPL[3]:1
Possible fix, define
  f(::Int64, ::Int64)

```

but I do not understand the reason behind this behavior.

I would expect `f(x::Union{Float64, Int64}, y::Int64)` to be more specific than `f(x::Int64, y)` as mentally `f(x::Union{Float64, Int64}, y::Int64)` can be rewritten as two identical separate definitions `f(x::Int64, y::Int64)` and `f(x::Float64, y::Int64)`.

Could someone please explain me the reason for the current behavior? Thank you.

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [April 12, 2019, 5:36pm UTC](https://discourse.julialang.org/t/method-ambiguity-for-unions/23086/2 "2019-04-12T17:36:09Z")

</div>

You just have two definitions:

1. `f(x::Union{Float64, Int64}, y::Int64)`
2. `f(x::Int64, y)`

When you call `f(x::Int, y::Int)`, method 2 is more specific for `x` but method 1 is more specific for `y`. Thus the ambiguity.

---

<div class="post-metadata">

### Author: ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)
#### Post date: [April 12, 2019, 5:45pm UTC](https://discourse.julialang.org/t/method-ambiguity-for-unions/23086/3 "2019-04-12T17:45:34Z")

</div>

Thank you. I understand this point, and of course `f(x::Union{Float64, Int64}, y::Int64)` has still to be ambiguous with `f(x::Int64, y::Union{Float64, Int64})` for two `Int`s passed.

My thinking was that when you have an ambiguity over an union vs ambiguity over an an abstract type then we could have a precedence defined over them (but I guess then the dispatch rules would get overly complex).
