Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overload += in Julia?

I'm trying to understand how operator overloading works in Julia. The manual is quite brief, and gives +() as an example function, then states all the operators are overloadable with their obvious names (a list of non-obvious names is also provided).

But what about +=? The function +=() doesn't even seem to exist, nor does +=!() (since it is a modifying function). I frequently overload operators in C++ by defining += first and then use a simple + based on copy and +=.

In my case I don't even think I need +, just the behavior of +=... I realize I could write my own modifying function but the operator syntax would be nice. (Out of curiosity, how do *=, /=, $=, etc work?)

like image 672
AndyF Avatar asked Feb 11 '23 04:02

AndyF


1 Answers

There is no += function. It is simply syntactic sugar for a = a + b.

It is also not mutating. So a += b calculates a + b and then changes a to refer to the result. This means there is memory allocation for the result of a + b.

like image 56
Mr Alpha Avatar answered Feb 19 '23 04:02

Mr Alpha