# ERROR: TypeError: non-boolean (Missing) used in boolean context

**URL:** <https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152>\
**Category:** General Usage\
**Created:** [June 21, 2022, 10:40pm UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152 "2022-06-21T22:40:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 21, 2022, 10:40pm UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152/1 "2022-06-21T22:40:53Z")

</div>

Why does this happen?

```julia
julia> 3 < missing < 5
ERROR: TypeError: non-boolean (Missing) used in boolean context

```

Is it expanding to this?

```julia
julia> (3 < missing) && (missing < 5)
ERROR: TypeError: non-boolean (Missing) used in boolean context

```

This one works

```julia
julia> (3 < missing) & (missing < 5)
missing

```

Would it make sense to change it so that this works?

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [June 21, 2022, 11:21pm UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152/2 "2022-06-21T23:21:08Z")

</div>

> [@jar1](#):
>
> Is it expanding to this?

You right, the _error_ is occurring because operations on `Missing` (such as comparisons in this case) results in `missing`, and using a type other than `Bool` with boolean operators like `&&` results in an error. So essentially, the expression: `3 < missing < 5` is expanded to `(3 < missing) && (missing < 5)`, which expands to `missing && missing`, and that’s what results in the error.

> [@jar1](#):
>
> This one works

The reason that `(3 < missing) & (missing < 5)` works its because `&` is the _bitwise and_ operator, which is not equal to the logical _and_ operator (`&&`). So essentially, using operations like `&&` and `||` with objects that are not `Bool`ean results in a error, and performing any operation (other than boolean operations) on `missing` will, generally, result in `missing`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 21, 2022, 11:27pm UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152/3 "2022-06-21T23:27:47Z")

</div>

I think the documentation is clear:

```julia
help?> &&
search: &&

  x && y

  Short-circuiting boolean AND.

help?> &
search: & &&

  x & y

  Bitwise and. Implements three-valued logic
  (https://en.wikipedia.org/wiki/Three-valued_logic), returning missing if one
  operand is missing and the other is true. Add parentheses for function application
  form: (&)(x, y).

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> 4 & 10
  0
  
  julia> 4 & 12
  4
  
  julia> true & missing
  missing
  
  julia> false & missing
  false

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [June 21, 2022, 11:33pm UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152/4 "2022-06-21T23:33:46Z")

</div>

> Chained comparisons use the && operator for scalar comparisons, and the & operator for elementwise comparisons, which allows them to work on arrays. For example, 0 .\< A .\< 1 gives a boolean array whose entries are true where the corresponding elements of A are between 0 and 1.

[https://docs.julialang.org/en/v1/manual/mathematical-operations/#Chaining-comparisons](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Chaining-comparisons)

What is the advantage of `&&` for this? Why not just use `&` in both cases? To handle a `missing` I can’t use chaining, because it fails with `&&`, but it would work fine with `&`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 22, 2022, 12:52am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152/5 "2022-06-22T00:52:36Z")

</div>

uhh, if you want an easy way you can just use the broadcast:

```julia
julia> 1 .< missing .< 5
missing

julia> 1 .< 2 .< 5
true

```

As numbers are containers of themselves this works.

The advantage is being what is expected, I believe. At least, I expected `1 < x < 5` to be expanded to `(1 < x) && (x < 5)`. `&` is for bitwise comparison so it would be unexpected to see it there. What is a little strange to me is that even with `&&` being short-circuiting the double constraint is not.

```julia
julia> 1 < 2 < error("a")
ERROR: a
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] top-level scope at REPL[4]:1

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [June 22, 2022, 1:56am UTC](https://discourse.julialang.org/t/error-typeerror-non-boolean-missing-used-in-boolean-context/83152/6 "2022-06-22T01:56:59Z")

</div>

I think this is a footgun. It’s not obvious that ` 1 < missing < 2` lowers to `(1 < missing) && (missing < 2)`. I get the appeal of short circuiting in this instance, but it’s still unexpected for new users.

It’s frustrating that we have `&&`, which errors with `missing` and `&` which has unexpected behavior with integers etc, but nothing which is _just_ for booleans _and_ handles `missing`.
