Overloading ≫ yields ERROR: TypeError: non-boolean used in boolean context

Can anyone help why this weird error happens? (working on julia 1.6.1)

julia> a ≫  b = a + b
≫ (generic function with 1 method)

julia> (1 ≫  2) ≫  3
6

julia> 1 ≫  (2 ≫  3)
6

julia> 1 ≫  2 ≫  3
ERROR: TypeError: non-boolean (Int64) used in boolean context
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

Of course for me it would be awesome to just not having the error, but I guess that is not possible. Hence does someone know a similar unicode operator which does not yield this error?

Try

julia> Meta.@lower 1 ≫ 2 ≫ 3
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = 1 ≫ 2
└──      goto #3 if not %1
2 ─ %3 = 2 ≫ 3
└──      return %3
3 ─      return false
))))

I have no idea why it lowers to a conditional go to based on the result of 1 ≫ 2. With brackets it works though.

julia> (1 ≫ 2) ≫ 3
6

julia> 1 ≫ (2 ≫ 3)
6
1 Like

thank you for the lowering hint, I goes it is because this operator has the name “greater greater” and someone put “greater” comparison semantics onto it

a bit unfortunate as I wanted to use it for monad composition, where this unicode is quite commonly used as an operator

This is to support chaining of comparisons. See also this issue. As noted there you’d probably have to use a macro to work around the lowering.

2 Likes