# Union{T,Nothing} vs multiple dispatch

**URL:** <https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414>\
**Category:** General Usage\
**Tags:** question\
**Created:** [November 17, 2022, 7:46pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414 "2022-11-17T19:46:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [November 17, 2022, 7:46pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/1 "2022-11-17T19:46:00Z")

</div>

For performance reasons, it is advised to avoid Unions. But on the topic of `Nothing`, the documentation suggests the compiler will emit efficient code. Is it preferred to use multiple dispatch, i.e., have two versions of the function, one with the argument and one without, or to use Union{T,Nothing} and perform an explicit check, e.g.,

```julia
if T === nothing

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 17, 2022, 7:53pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/2 "2022-11-17T19:53:48Z")

</div>

> [@prittjam](#):
>
> one with the argument and one without

A function with no argument is not the same as a function which receives `nothing` as an argument:

```julia
julia> f() = 1
f (generic function with 1 method)

julia> f(x) = 2
f (generic function with 2 methods)

julia> f()
1

julia> f(nothing)
2

```

So that is not exactly the question. But yes, you can use `if x == nothing` and that will produce efficient code.

```julia
julia> function g(x)
           if x == nothing
               return 1
           else
               return 2
           end
       end
g (generic function with 1 method)

julia> @code_llvm g(nothing)
; @ REPL[10]:1 within `g`
define i64 @julia_g_489() #0 {
top:
; @ REPL[10]:3 within `g`
  ret i64 1
}

julia> @code_llvm g(1)
; @ REPL[10]:1 within `g`
define i64 @julia_g_491(i64 signext %0) #0 {
top:
; @ REPL[10]:5 within `g`
  ret i64 2
}

```

(there are not branches in the function - but that is not quite specific of `Nothing`, if the branch can be inferred from the input **type** , the compiler can very well create methods that avoid the branching for each type).

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [November 17, 2022, 8:10pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/3 "2022-11-17T20:10:21Z")

</div>

What is preferred: to have two methods that dispatch on the type or one signature that uses a Union and tests for nothing?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 17, 2022, 8:11pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/4 "2022-11-17T20:11:00Z")

</div>

> [@lmiq](#):
>
> `if x == nothing`

Note: you want `===` not `==`, since arbitrary things could be `== nothing` but only `nothing` is `=== nothing`

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 17, 2022, 8:11pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/5 "2022-11-17T20:11:10Z")

</div>

I would personally prefer the two-signature version.

---

<div class="post-metadata">

**Author:** ![prittjam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prittjam/32/21267_2.png) [@prittjam](https://discourse.julialang.org/u/prittjam)\
**Post date:** [November 17, 2022, 8:11pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/6 "2022-11-17T20:11:48Z")

</div>

Reason being? stylistically or performance? I’ve seen examples of both approaches in Base.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 17, 2022, 8:32pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/7 "2022-11-17T20:32:34Z")

</div>

Stylistic. In this case, perf is clealry the same.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 17, 2022, 9:03pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/8 "2022-11-17T21:03:41Z")

</div>

> [@rdeits](#):
>
> Note: you want `===` not `==`, since arbitrary things could be `== nothing` but only `nothing` is `=== nothing`

Yeah, VSCode linters that and I change it to `isnothing(x)`

Ps. What can be `nothing` without being `Nothing`?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [November 17, 2022, 9:39pm UTC](https://discourse.julialang.org/t/union-t-nothing-vs-multiple-dispatch/90414/9 "2022-11-17T21:39:32Z")

</div>

> [@lmiq](#):
>
> Ps. What can be `nothing` without being `Nothing`?

Well, `==` is user-defined, so anything:

```julia
julia> struct MyType
         x::Int
       end

# This is probably a bad idea, but there's no reason you *can't* do it
julia> Base.:(==)(m::MyType, ::Nothing) = true

julia> MyType(1) == nothing
true

```

But `===` is always a test for actual equivalence, so even weird user code can’t override it:

```julia
julia> MyType(1) === nothing
false

```
