# Does \`==(::Missing, ::Any)=missing\` violate type stability?

**URL:** <https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649>\
**Category:** General Usage\
**Created:** [June 12, 2022, 11:15pm UTC](https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649 "2022-06-12T23:15:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [June 12, 2022, 11:15pm UTC](https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649/1 "2022-06-12T23:15:52Z")

</div>

(The intent of this question is to improve my understanding of type stability, not criticize the language design.)

Most comparisons, like `0==0` and `false==1` and so on return a `Bool`, but comparisons with `missing` return `missing` by design. This creates a gotcha in writing generic code with if statements, because for arbitrary input `a==b` could be `Bool` or `Missing` (and therefore `if a==b` could be an error). Doesn’t this violate the principles of type stability?

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 12, 2022, 11:30pm UTC](https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649/2 "2022-06-12T23:30:00Z")

</div>

Type stability means if the return type can predicted from the _types_ of the arguments. No this does not violate type stability. If one the arguments are of type `Missing` then you can predict the return type will be `Missing`.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [June 12, 2022, 11:43pm UTC](https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649/3 "2022-06-12T23:43:25Z")

</div>

To add on this, this is the conventional notion of type stability as a property of a method: given concrete types in, known return type out. `==(::Missing, ::Any)` is type-stable because just given the first concrete `missing`, it could only return `missing`.

A looser notion of type stability applies to whether a _variable_ or _expression_ can be inferred to have a fixed concrete type at compile-time. When people are being precise, they say “inferable”/“has an inferable type” rather than “type-stable”, though it is accurate to say that the return type of a type-stable method is inferable.

I think the latter is what OP is getting at. If `a` is known to be `missing`, then the expression `a==b` is known to be `missing`. But if `a` is not inferable because it could be `1` or `"foo"`, `a==b` is not inferable either and will dispatch to `==` methods at run-time.

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [June 13, 2022, 12:47am UTC](https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649/4 "2022-06-13T00:47:20Z")

</div>

In other words, if I write code containing an instance of `a==b` that could dispatch to either `==(::Any, ::Any)` or `==(::Missing, ::Any)` at runtime, then _my code_ might be bad, but this doesn’t mean that the methods themselves are type unstable?

---

<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:** [June 13, 2022, 1:17am UTC](https://discourse.julialang.org/t/does-missing-any-missing-violate-type-stability/82649/5 "2022-06-13T01:17:53Z")

</div>

Yeah, that’s exactly right. If your code looks like:

```julia
function foo()
  if rand() < 0.5
    x = "hello"
  else
    x = missing
  end

  if x == missing
    do_stuff()
  end
end

```

then the type of `x` can’t be concretely inferred, but it’s not `==`'s fault.
