# Best practices for dealing with \`Union{Nothing,...}\`

**URL:** <https://discourse.julialang.org/t/best-practices-for-dealing-with-union-nothing/27402>\
**Category:** New to Julia\
**Created:** [August 10, 2019, 10:30pm UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-union-nothing/27402 "2019-08-10T22:30:09Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 12, 2019, 1:20am UTC](https://discourse.julialang.org/t/best-practices-for-dealing-with-union-nothing/27402/14 "2019-08-12T01:20:17Z")

</div>

> [@rdeits](#):
>
> there’s nothing magic about `nothing` in particular

I also want to add that difference in inference of `isnothing` vs `=== nothing` also applies to `missing` (which is a singleton as well). Keno explains why that’s the case in [ismissing(x) much slower than x === missing · Issue #27681 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/27681#issuecomment-399054971)

> `===` is special in inference and inference happens before inlining

Note that `isa` is also special; so `x === nothing` should also be as good as `x isa Nothing`.

> [@banbh](#):
>
> I thought I would “monadify” it as follows:
> 
> ```julia
> function bnd(f, x)
> x === nothing ? nothing : f(x)
> end
> 
> ```

Regarding monadic style, I don’t think it is regarded as “bad” in Julia. For example, there is a discussion about adding a special syntax to lift a function on `Union{T, Missing}`: [Status of question mark syntax for missing values - #2 by StefanKarpinski](https://discourse.julialang.org/t/status-of-question-mark-syntax-for-missing-values/27189/2)

But my impression is writing something like

```julia
i1 = findfirst(isequal(c1), labels1)
i1 === nothing && return nothing
i2 = findfirst(isequal(c2), labels2)
i2 === nothing && return nothing
return (i1, i2)

```

is more common. This is so common that IterTools.jl has [`@ifsomething`](https://juliacollections.github.io/IterTools.jl/latest/#IterTools.@ifsomething) which can be used to write

```julia
i1 = @ifsomething findfirst(isequal(c1), labels1)
i2 = @ifsomething findfirst(isequal(c2), labels2)
return (i1, i2)

```

This expands to something like above.

---

_[View the full topic](https://discourse.julialang.org/t/best-practices-for-dealing-with-union-nothing/27402)._
