Missing <= 2 <= 3 does not work

julia> f() = missing <= 2 <= 3
f (generic function with 1 method)

julia> @code_lowered f()
CodeInfo(
1 ─ %1 = Main.missing <= 2
└──      goto #3 if not %1
2 ─ %3 = 2 <= 3
└──      return %3        
3 ─      return false
)

julia> f()
ERROR: TypeError: non-boolean (Missing) used in boolean contextStacktrace:
 [1] f() @ Main .\REPL[19]:1
 [2] top-level scope @ REPL[21]:1

Chained comparisons “use the && operator for scalar comparisons”. This means that your code is equivalent to something like:

missing <= 2 && 2 <= 3

which fails because the && short-circuit operator cannot handle missing.

Just explicitly write it out and use &, ala (missing <= 2) & (2 <= 3). Note that parentheses are needed because & has higher precedence than <=.

4 Likes

Of course… I’ve seen it, too much syntax lets me forget meaning.

I see why it doesn’t work and how to prevent it. But shouldn’t we fix it? Seems inconsistent

It seems like fixing it would require changing chained comparisons to use & instead of &&. This has been discussed previously in

and the conclusion by @jeff.bezanson in the latter issue was:

Unfortunately I don’t see a non-breaking way to implement this.

which means it will have to wait until 2.0 (if ever)?

Can you file an issue at MissingsAsFalse.jl? Currently the @mfalse macro does not support this chained comparison, but it would be a good addition.