Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"...

like image 421
Mateo Avatar asked Oct 29 '25 17:10

Mateo


1 Answers

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.

like image 124
ivarne Avatar answered Oct 31 '25 10:10

ivarne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!