Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Scala Tutorial Part 4 – Advanced Features of Scala

Scala Tutorial Part 4 – Advanced Features of Scala

 July 15  | 0 Comments

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.

Basics of Scala

Control Structures and Functions in Scala

Introduction to Classes and Objects in Scala

Hadoop

Pattern Matching

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

Syntax:

expr match {
case pattern1 => expression1
case pattern2 => expression2
-
-
-
case patternn => expressionn
}

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.

 

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 _,

 

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.

Example

def parseInt(s:String) : Int = try {
s.toInt
} catch {
case e: NumberFormatException => 0
} finally {
// finally block code
}

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.

Option[T]

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

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

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

Syntax:

 var addMore = (x:Int) =>x + more

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.

 


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


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.

 

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.


We hope this blog helped you in understanding the advanced features of Scala. In our next blog on Scala series we will be discussing about the Collections in Scala. Keep visiting our website www.acadgild.com for more updates on Big data and other technologies. Click here to learn Bigdata Hadoop from our Expert Mentors

>