# Wield behavior of multiple dispatch

**URL:** https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986
**Category:** General Usage
**Tags:** multiple-dispatch
**Created:** [November 29, 2022, 10:54am UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986 "2022-11-29T10:54:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)
#### Post date: [November 29, 2022, 10:54am UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/1 "2022-11-29T10:54:15Z")

</div>

In Julia 1.8.3. Given the following two type signatures.

```julia
(x::AbstractFloat, y::Number)
(x::Union{Float64, ComplexF64}, y::Union{Float64, ComplexF64})

```

I would expect there being an ambiguity error if the input type is `(::Float64, ::Float64)`.

## But

```julia
julia> FloatAndComplex64 = Union{Float64, ComplexF64}
Union{Float64, ComplexF64}

julia> begin
               function roughly_equal(x::AbstractFloat, y::Number)
                       @info "(::AbstractFloat, ::Number)"
                       -10 * eps(x) < x - y < 10 * eps(x)
               end
               function roughly_equal(x::Union{Float64, ComplexF64}, y::Union{Float64, ComplexF64})
                       @info "(::Union{Float64, ComplexF64}, ::Union{Float64, ComplexF64})"
                       abs(x - y) < 10 * eps(y)
               end
       end
roughly_equal (generic function with 4 methods)

julia> roughly_equal(3.0, 3.0)
[ Info: (::Union{Float64, ComplexF64}, ::Union{Float64, ComplexF64})
true

```

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [November 29, 2022, 11:12am UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/2 "2022-11-29T11:12:56Z")

</div>

The rules for multiple dispatch are quite complex. Apparently, here the compiler ranks a _union of concrete types_ higher than _abstract types_.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [November 29, 2022, 12:12pm UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/3 "2022-11-29T12:12:33Z")

</div>

Yes, the compiler considers the union to be more specific.

```julia
julia> Base.morespecific(Union{Float64, ComplexF64}, AbstractFloat)
true

julia> Base.morespecific(Union{Float64, ComplexF64}, Number)
true

```

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [November 29, 2022, 2:11pm UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/4 "2022-11-29T14:11:51Z")

</div>

On second thought, this can be understood using set theory: The union of concrete types is a subset of the abstract type because the abstract type includes any additional concrete subtype that is not included in the union.

---

<div class="post-metadata">

### Author: ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)
#### Post date: [November 29, 2022, 2:16pm UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/5 "2022-11-29T14:16:00Z")

</div>

Yeah, it makes sense. But it turns out the union of abstract types can also more concrete

```julia
julia> Base.morespecific(Union{AbstractFloat, Complex}, Real)
true

```

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [November 29, 2022, 2:19pm UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/6 "2022-11-29T14:19:22Z")

</div>

I gotta be honest that one makes no sense to me.

It also works with `Union{Int, AbstractString}` and `Number`.

---

<div class="post-metadata">

### Author: ![jg-854](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jg-854/32/20395_2.png) [@jg-854](https://discourse.julialang.org/u/jg-854)
#### Post date: [November 29, 2022, 3:24pm UTC](https://discourse.julialang.org/t/wield-behavior-of-multiple-dispatch/90986/7 "2022-11-29T15:24:20Z")

</div>

> [@josuagrw](#):
>
> The union of concrete types is a subset of the abstract type because the abstract type includes any additional concrete subtype that is not included in the union.

This argument only applies to `y`:

```julia
julia> Union{Float64, ComplexF64} <: Number
true

```

But it does not hold for `x`:

```julia
julia> Union{Float64, ComplexF64} <: AbstractFloat
false

```
