O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required