Mateo
Mateo

Reputation: 1604

How does one overload / redefine binary operators in Julia?

I keep losing my reference on how to redefine say the || ("or") or && ("and") binary operators. I read somewhere that one has to first do importall Base . Then I tried

Base.||( x::MyType, y::MyType ) = dosomething( x, y )

and also

Base.or( x::MyType, y::MyType ) = dosomething( x, y ) 

But none of these worked. I would appreciate if somebody could give a reference explaining the basics of how to do this... I was unable to find one with queries such as "redefine binary operator in Julia"...

Upvotes: 4

Views: 1998

Answers (1)

ivarne
ivarne

Reputation: 3783

As && and || are short circuit operations, they can't be overloaded, without adding a special construction separate from functions. To call a function, you need to evaluate all the arguments, and that would not be a short circuit evaluation.

You might be able to overload & and | instead, but it is hard to tell without an example use case.

Upvotes: 5

Related Questions