# Ismissing(x) versus x === missing

**URL:** <https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171>\
**Category:** General Usage\
**Created:** [December 21, 2020, 1:30pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171 "2020-12-21T13:30:16Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [December 21, 2020, 1:30pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/1 "2020-12-21T13:30:16Z")

</div>

Maybe this is a dumb question, but there seems to be a bit of confusion about using `ismissing(x)` versus `x === missing`, and `isnothing(x)` versus `x === nothing` at [[ANN] ShowLint - #3 by Mason](https://discourse.julialang.org/t/ann-showlint/52140/3).

I would say that in both cases, the functions are clearer for new Julia users. This is supported by the documentation for missing values ([Missing Values · The Julia Language](https://docs.julialang.org/en/v1/manual/missing/#Equality-and-Comparison-Operators)).

On the other hand, some people say that the function calls are slower, see [https://github.com/JuliaLang/julia/issues/35585](https://github.com/JuliaLang/julia/issues/35585) and and [https://github.com/JuliaLang/julia/issues/27681](https://github.com/JuliaLang/julia/issues/27681).

So, is it right that `ismissing` and `isnothing` is prefered except when you need to squeeze every last bit of performance?

---

<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:** [December 21, 2020, 1:34pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/2 "2020-12-21T13:34:43Z")

</div>

> [@rikh](#):
>
> So, is it right that `ismissing` and `isnothing` is prefered except when you need to squeeze every last bit of performance?

Using `===` (or `!==`) allows the compiler to disregard the other alternative type if the inferred type is e.g. a `Union{Nothing, T}`. Using `==` or `isnothing` does not allow this. [https://github.com/JuliaLang/julia/pull/38905](https://github.com/JuliaLang/julia/pull/38905) might fix this though.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [December 21, 2020, 1:45pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/3 "2020-12-21T13:45:13Z")

</div>

> [@rikh](#):
>
> I would say that in both cases, the functions are clearer for new Julia users.

But `Lint` is not for new users only. I don’t think `isnothing(x)` should be advised against `x === nothing`. I have no intention to follow the suggestion and it would be another annoying red mark in the code when opened in VSC.

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [December 21, 2020, 2:44pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/4 "2020-12-21T14:44:45Z")

</div>

I’ve replied to this at [[ANN] ShowLint - #11 by rikh](https://discourse.julialang.org/t/ann-showlint/52140/11).

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 21, 2020, 4:52pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/5 "2020-12-21T16:52:49Z")

</div>

> [@joa-quim](#):
>
> it would be another annoying red mark in the code when opened in VSC.

@joa-quim To clarify, the linter that @rikh has created is separate from the linter that is built into the Julia VS Code extension.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 21, 2020, 4:58pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/6 "2020-12-21T16:58:21Z")

</div>

> [@kristoffer.carlsson](#):
>
> Using `==` or `isnothing` does not allow this.

Is that because if `x` is of type `T` (rather than `Nothing`), `isnothing(x)` might not return a boolean value? In other words, `isnothing(x::T)` might be overloaded to not return a boolean? Or perhaps `isnothing(x::T)` is overloaded and for some reason the compiler cannot infer the return type of `isnothing(x::T)`? If the compiler can correctly infer the return type of `isnothing(x::T)`, then I wouldn’t think that there would be a performance hit…

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [December 21, 2020, 6:06pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/7 "2020-12-21T18:06:51Z")

</div>

Sorry for **my** dumb addition. I use this in my packages for Julia 1.0 compat (which is the implementation in Julia 1.1+):

```julia
isnothing(::Any) = false
isnothing(::Nothing) = true

```

So actually, this:

```julia
@inline isnothing(x) = x === nothing

```

would be the more performant (compiler friendly) version which has no drawbacks. Why is it then implemented like above? Or do I miss anything obvious?

---

<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:** [December 21, 2020, 6:50pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/8 "2020-12-21T18:50:50Z")

</div>

> [@tamasgal](#):
>
> So actually, this:
> 
> ```julia
> @inline isnothing(x) = x === nothing
> 
> ```
> 
> would be the more performant (compiler friendly) version which has no drawbacks.

No, it has to be `===` at every call site in the code. As soon as you hide it behind a function, you’ve lost the `===` advantage.

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [December 21, 2020, 6:51pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/9 "2020-12-21T18:51:22Z")

</div>

Even if it’s inlined? 🤔

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [December 21, 2020, 7:00pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/10 "2020-12-21T19:00:11Z")

</div>

> [@kristoffer.carlsson](#):
>
> As soon as you hide it behind a function, you’ve lost the `===` advantage.

This is rather confusing, because Julia is often advertised as having “zero cost abstractions”.

---

<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:** [December 21, 2020, 10:22pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/11 "2020-12-21T22:22:58Z")

</div>

> [@tamasgal](#):
>
> Even if it’s inlined? 🤔

Yes.

> [@CameronBieganek](#):
>
> This is rather confusing, because Julia is often advertised as having “zero cost abstractions”.

[![](https://global.discourse-cdn.com/julialang/original/3X/b/8/b84bc25efdff4d1ad7caecb928d44893957d481a.jpeg "CppCon 2019: Chandler Carruth “There Are No Zero-cost Abstractions”") ](https://www.youtube.com/watch?v=rHIkrotSwcc)

😉

As already been said, there is WIP to fix this ([https://github.com/JuliaLang/julia/pull/38905](https://github.com/JuliaLang/julia/pull/38905)).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 21, 2020, 10:55pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/12 "2020-12-21T22:55:44Z")

</div>

Are there really no zero-cost abstractions? I thought some abstractions ‘compiled away’ to produce the same machine code as the ‘inline’ code (like in inlined functions).

Are you including compilation cost?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 21, 2020, 11:05pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/13 "2020-12-21T23:05:20Z")

</div>

The TLDR of the talk is that there are 3 classes of cost: runtime, compile, and human. I think Julia does have a better argument for (basically) 0 cost abstractions in some cases. Our macros are a masterpiece of engineering, but still there is some nonzero compile time cost.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 21, 2020, 11:53pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/14 "2020-12-21T23:53:18Z")

</div>

Admittedly, I didn’t watch the video. When I think of zero-cost abstractions, I basically mean run-time cost. I have the impression that this is pretty common.

---

<div class="post-metadata">

**Author:** ![bert](https://avatars.discourse-cdn.com/v4/letter/b/9e8a1a/32.png) [@bert](https://discourse.julialang.org/u/bert)\
**Post date:** [May 26, 2021, 11:15pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/15 "2021-05-26T23:15:19Z")

</div>

Currently Julia has a unary `==` function defined so that `==(x)` is `y -> y == x`. Why not also have a unary version of `===` so that `isnothing` can become `===(nothing)`, and the same for `ismissing`? This way nobody would be tempted to use `===(value, nothing)` instead of the (superior?) `value === nothing` because the latter is obviously “more” correct. In comparison, it’s less clear at a glance which of `isnothing(value)` and `value === nothing` is preferable.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [May 26, 2021, 11:33pm UTC](https://discourse.julialang.org/t/ismissing-x-versus-x-missing/52171/16 "2021-05-26T23:33:03Z")

</div>

This does not make any difference in the curried form, since you are already introducing a function barrier which will inhibit constraint propagation anyways. The PR mentioned above has been merged though and will be in Julia 1.7, so the advice of always using `x === missing` will be obsolete soon.
