How to overload + = in Julia?

I am trying to understand how operator overloading works in Julia. The manual is quite short and gives +()

as an example function then states that all operators are overloaded with their obvious names (a list of non-obvious names is also provided).

But what about +=?

Function +=()

doesn't even exist, and doesn't work +=!()

(since it's a modifying function). I often overload operators in C ++ by first defining +=

and then using a simple +

copy-based and +=

.

In my case, I don't even think what I need +

, just the behavior +=

... I understand that I can write my own modifying function, but the operator syntax will be nice. (Out of curiosity, how they work *=

, /=

, $=

etc?)

+3


source to share


1 answer


No function +=

. It's just syntactic sugar for a = a + b

.



It also doesn't mutate. So a += b

calculates a + b

and then changes a

to refer to the result. This means that a + b

there is a memory allocation for the result .

+7


source







All Articles