Scala Foundation Course - Scala Operators


We learned about the fundamentals of Scala type system in the previous lesson. The next thing is the operators. When you learned your first programming language, you must have learned at least three categories of operators.

  1. Arithmetic operators
  2. Logical operators
  3. Bitwise operators.

Then you might have also learned about operator precedence and associativity rules. Most of that skill is still valid in Scala, and I don't see a need to repeat all of that. However, I will focus on the differentiating features of Scala operators.
The first feature is this.

Scala doesn't have any operator

Shocked? Let me explain.
Scala is a pure object-oriented language because every value in Scala is an Object. You create an Integer, and It is an Object. You create a Double or a String, and they are objects. So, if they are objects, they may have some methods. Right? Let's look at the documentation for Integer
Look at the documentation and you will find that = symbols used as a method identifier. Does it look strange? I mean, have you seen symbols as method names? That's unusual. Right?
Scroll down, and you will see most of the operators defined as a method. The multiplication, the addition, the less than and much more.
Keep scrolling down, and you will find some usual method identifiers as well. The abs method and the compare method. They appear to be standard methods. Right? You must have seen that kind of methods in Java or C++.
Similarly, if you check the documentation for the Double, you will find the same thing there as well. So, the point that I want to make is this.

Scala allows us to use the symbols as identifiers. And Scala implements the arithmetic, logical and bitwise operators as methods.

The next question is, how do we call these methods? Well, you can call them just like you call a method in any other object-oriented language. I mean, using the dot notation.


                                    
    val i = 10                                          
                             

The identifier i is an Integer object. Now, you can call a plus method using a dot notation.

                                        
    i.+(15)                                          
                                 

The result is 25. So, we used an addition operator. Similarly, you can use other methods such as multiplication, greater than, abs, and compare.

                                        
    i.*(5)
    i.>(5)
    i.abs
    i.compare(15)                                       
                                 

But this approach of using an operator is awkward. You may be happy to use abs or compare method using a dot notation, but for arithmetic or logical operations, this approach is not only inconvenient but also doesnot follow mathematical notation.The mathematical notation is to use it like below.

                                        
    i + 15
    i * 5
    i > 5                                        
                                 

Scala allows the above syntax as well.The good thing is that the mathematical notation also applies to usual methods. Let me show you.

                                        
    i compare 5
    i compare 15                                        
                                 

Well, it doesn't look nice to use compare as an infix mathematical operator, but people do use this syntax because sometimes it makes more sense. Here is an example.

                                        
    i to 20                                        
                                 

In this example, to is a method that returns a Range collection. I prefer to use it as an operator rather than a method (shown below).

                                        
    i.to(20)                                       
                                 

So, the point that I wanted to make is this.

Every value in Scala is an Object. Every method in Scala is an Operator.

Scala allows object-oriented notation (the dot notation) as well as the operator notation for making a method call. It is up to you to choose the preferred notation for a given scenario.


Scala Operator Notation

There are few more minute details of using mathematical notation. Let me cover that as well.
All the methods that we used so far were taking just one input parameter. Look at the addition operator. It takes only one parameter, and we use it like this.

                                        
    i to 20                                       
                                 

The object, then the method and finally the input parameter. So, the method sits in between the object and the input parameter.
We can represent the syntax like this.

                                        
    <object><method><parameter>            
                    

So far so good. But what will happen for following scenarios?

  1. We have more than one input parameter.
  2. We don't have any input parameter.

The answer is straightforward. We can still use the same syntax. The only difference is that if we have more than one parameter, we place a parenthesis around them as shown below.

                                        
    <object><method> (<parameter-1> [, <parameter-2>, ...] )                                       
                                 

Let me show you an example.

                                        
    i to (20,2)                                      
                                 

We have already seen to method. Right? It is overloaded to take two parameters. The second parameter is the step size. So, if we have two or more parameters, we must use a parenthesis to enclose them.
What if we don't have any parameter? For example, abs method on the Integer object. We can still use the same notation. The only difference is that you do not pass the parameter.

                                        
    i abs                                      
                                 

Another example is shown below.


                                        
    val s = "my name is ..."
    s toUpperCase                                           
                                 

You might want to prefer the dot notation for such use cases.

                                        
    s.toUpperCase                                           
                                 

Okay, the final pending question.
What about the unary operator? For example,

                                        
    -i                                          
                                 

This example applies a negative unary operator to the identifier i. If you check the documentation and search for unary, you will realize that Scala defines them as unary_ and the symbol. You can use them as prefix unary operator as we use it in mathematics.

                                        
    -i * 2                                        
                                 

You can also use the dot notation and call them as methods.

                                        
    i.unary_-                                        
                                 

I am sure you won't like to use unary operator using a dot notation. The final note.
Scala allows only four symbols as a unary operator.
+, -, !, and ~
No other symbol can be used a unary operator in Scala.


Great! The main take away from this session is that the operators in Scala are method calls. You can say it the other way. Methods in Scala are operators. All you need to do is to look at the Object documentation and use the listed methods like an operator or simply like a method.
Thank you for watching Learning Journal. Keep Learning and Keep growing.


You will also like:


Scala Functions

Scala is a functional programming language. Functions are the building blocks in Scala.

Learning Journal

First Class Functions

Function is a first-class citizen in functional programming. What does it mean?

Learning Journal

Hadoop Security

Hadoop security implementation using Kerberos.

Learning Journal

Scala placeholder syntax

What is a scala placeholder syntax and why do we need it? Learn from experts.

Learning Journal

Spark in Google cloud

Learn How to Install Hadoop and Spark in Google Cloud in just 2 minuts.

Learning Journal