Scala Tutorial Part 4 – Advanced Features of Scala

Scala Tutorial Part 1- Basics of Scala

Scala Tutorial Part 2- Basics of Scala

Scala Tutorial Part 3 – Introduction to Classes and Objects in Scala

In this blog, we will discuss about the advanced features of Scala like Pattern Matching, Exception Handling, Option[T], Closure, File Operations.

Before going through this blog, we recommend you to got through our previous blogs on Scala which will be helpful to get the concepts discussed ahead.

Pattern Matching

It is a generalization of switch statement in C or Java, to handle the class hierarchies.


In Scala, instead of switch, there is a standard method called match. The syntax above is used to write pattern matching in Scala which looks similar to switch statement.

No break statement is required after a case because each clause is self-contained here. It returns the value of the first matched alternative.

Patterns can be constructed from:

  • Literals
  • constant identifiers
  • the “wildcard” pattern _,
  • pattern variables
  • Case class constructors

Below is an example on how to use match method to match your expressions like switch statement.

Pattern matching in scala

In the above example you can see that it is similar to switch statement and we are calling to print the case 1 & 3 which are January and March.

In switch statements, we have something called default case which will be called whenever we give a case which is out of the statement. In Scala, this default case is possible with the “wildcard” pattern _,

wild card pattern

In the above example, you can see that we are calling case 13 which is not a valid expression. In our expression, we have added something called wildcard because of which, it will print the output as invalid input.

In Scala, pattern matching is not restricted to one data type per match. We can have multiple types of case statement in a single match. Refer the screen shot above for the same. You can also see that in spite of passing multiple types of data into the match method, it can still execute the expressions.

Exception Handling

Exception handling in Scala is similar to exception handing in Java. Let us assume that there is an exception. The exception will be detected in the try block and catch block will catch the exception and will be thrown using the throw keyword which is similar to Java. We have a block in Scala too, which will be executed regardless of the exception. In Scala, we have an advanced use of catch block. We can use catch block for pattern matching, so that we can write as many cases as we want. In Scala, all the exceptions are unchecked.

Here is a simple example for exception handling in Scala.


Scala imports the exception handling features from Java. So, it imports all the Java exception handling packages in order to perform exception handling in Scala.


Marks functions, may or may not return a result.

• Has two implementations

  • None

  • Some(t)

• Similar to a collection of one item at the most.

• Use Option instead of Null to avoid Null Pointer Exceptions

option in scala

Let us define one variable as Some and let us see what are the various options available with that variable.

In the above screen shot, you can see that we have defined a variable mysome as Some(1) and when we try to check the different methods that can be applied on the variable, we get the options.


Closure is a function where the computation depends on variables that are outside its scope.


Here x is a bound variable, more is a free variable. Now the resulting function will contain a reference to the free variable and is computed each time.

Closure in scala

Let us see the same in Scala shell.

Here, you can see that when we first define the addMore function, it returns an error that moreis not found. Later, we define more as 2. Now, when we try to declare the function addMore, we have successfully declared it. When we pass the argument 2 to the addMore function, it takes 2 from the variable, more and adds it with the argument that we pass and returns the result as 4.

File Operations

Scala provides a class called Source to perform different types of operations on files. If the size of the file is large, Scala provides a BufferedSource class which reads the data, serially from the file by configuring a buffer.

We use the method fromFile to read the text inside a file. We will now see an example on reading text from the files.

File operations
In the above screen shot, you can see that we have imported the Source class from the Scala io and by using the fromFile method, we have read the content of the file.

FIle operation methods

In the below screen shot you can see the different uses of fromFile method to read the content of the file.

In the below screen shot, you can find the different methods that are available to convert the data and to perform operations on your data.

File operations io
We hope this blog helped you in understanding the advanced features of Scala.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s