Abusing operators

was surprised to find this is a valid Julia code:

julia> ==(==,==)
true

This fails, though:

julia> == == ==
ERROR: syntax: "==" is not a unary operator

I think both should parse the same, not that anyone sane would write either expression (hmm…)

You just need parentheses:

julia> (==) == (==)
true

This isn’t really abuse — nearly all “operators” are functions themselves and can be called as such. They just have special parsing that can make it tricky for Julia to see the function call as a function call or the infix operator syntax as infix syntax, so parentheses are sometimes required to disambiguate.

6 Likes

Or even

julia> (==) == ==
true

just to prevent the first == from being tried as a unary operator.