Operator Overloading
Technically, Scala has no operators, so operator overloading means overloading symbols like +, +-, and so on. In Scala, these are method names. Operators take advantage of Scala’s lenient method invocation syntax—Scala does not require a dot (.) between the object reference and method name.
These two features combined give the illusion of operator overloading. So, when you call ref1 + ref2, you’re actually writing ref1.+(ref2), and invoking the + method on ref1.
Let’s look at an example of providing the + operator on a Complex class that represents complex numbers. Complex numbers, as we know, have a real part and an imaginary part, and they’re useful in computing complex equations that involve the square root of negative ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access