Scala Operator Magic

Published by on
Tags:

I have been looking a learning Scala and the other day I came across this code example on the Scala site. Initially this took a little time to understand so I thought I would blog what is going on so that I don’t forget and hopefully I save someone else some time.

So the code example I am using is below, it’s a slight modification of the code example found on the Scala site:

    1 object complexOps extends Application {

    2   class Complex(val re: Double, val im: Double) {

    3     def + (that: Complex) =

    4       new Complex(re + that.re, im + that.im)

    5     def - (that: Complex) =

    6       new Complex(re - that.re, im - that.im)

    7     def * (that: Complex) =

    8       new Complex(re * that.re - im * that.im,

    9                   re * that.im + im * that.re)

   10     def / (that: Complex) = {

   11       val denom = that.re * that.re + that.im * that.im   

   12       new Complex((re * that.re + im * that.im) / denom,

   13                   (im * that.re - re * that.im) / denom)

   14           }

   15     override def toString =

   16       re + " "+im

   17   }

   18 }

The code is quite simple, it just allows us to do basic operations on Complex objects. So lets add some code and start to get it working. First we need to define some Complex objects that contain some values, add this code after line 17 in the above example:

   18   var com1 = new Complex(1,2)

   19   var com2 = new Complex(3,4)

Now if we add a line to print out some data to the console:

   20   println(com1+com2)

Now save (as complexOps.scala) and compile this code:



And run it like so:



Ok so how did we get 4.0 and 6.0 as a result? Well lets start by looking at the print line:

   20   println(x+y)

What we are telling Scala to do is to use the add operator on the first Complex object, we are not concatenating the toString methods of each object;  we are calling these lines of code:

    3     def + (that: Complex) =

    4       new Complex(re + that.re, im + that.im)

This tells us that the operator can be applied when the other object is also a Complex object. The other object is stored in the variable "that" and we can now make use of it within the method body. As you can see from the line extract we create a new Complex object that use the sum of the original two objects as it's values.The new complex object is then returned, the final step is then to call the toString method. This as you can see will output the two values of our new Complex object to the screen. Pretty cool.

But thats not the end, we can overload this!!! oh yes. Lets define a second class:

   21   class MoreComplex(val re: double){

   22 

   23   }

As you can see really simple! Ok now on our original object lets add another definition for the add operator below the original add operator:

    2   class Complex(val re: Double, val im: Double) {

    3     def + (that: Complex) =

    4       new Complex(re + that.re, im + that.im)

    5     def + (that: MoreComplex) =

    6       new Complex(re+that.re, im)

Now lets create an instance of more complex and print out the result, adding it one of our original Complex objects:

   30   var com3 = new MoreComplex(5)

   31   println(com1+com3)

Ok lets compile this and see what we get as output:



Excellent the correct result! How awesome. With the example the Complex objectmust be on the left of the operator, if you swap the over you will receive and error because the MoreComplex class does not have any operators defined to handle a Complex object.

Have a play with the code and see what the other operators do!

Add comment

 

Projects

Have you read?

Sitecore's RESTful Security Risk - Part 2



Blogs by date