Bug in unary operator overloading syntax?

Why doesn’t the following code work? I’m using Julia 1.6.1

struct MyNumber
    value::Number
end
import Base.-
-(a::MyNumber)::MyNumber = MyNumber(-a.value)

ERROR: syntax: "a::MyNumber" is not a valid function argument name around 
REPL[3]:1
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

The error disappears if I remove the type annotation for the return value, i.e. changing the last line to

-(a::MyNumber) = MyNumber(-a.value)

Is there a good reason for this behavior?

To define functions for operators, you often have to quote them to avoid the parsing ambiguity of defining them vs. calling them:

Base.:-(a::MyNumber)::MyNumber = MyNumber(-a.value)
4 Likes