Sunday 21 August 2016

Scala's Option/Some/None Type In Depth (Part-1)

Post Brief Table of Content

  • Introduction
  • What is Option?
  • Advantages of Option
  • Basic Examples of Option
  • Scala Option Type’s getOrElse Method
  • Real-time Scenarios of Option
  • Scala’s Option Type In Pattern Matching

Introduction

In this post, I’m going to discuss one of the important concept of Scala Programming Language:Option type. We can also call it as Option/Some/None Pattern.
We will discuss about What is Option? What is Some or None? What is the use of this Option? What is the equivalent class available in Java SE 8? and some more questions in this post.

What is Option?

In Scala, Option is an abstract class used to represent exist or non-exist of a value. That means it is used to represent whether a value is present or absent.
In brief, Scala Language uses Option to indicate a value may or may not exist. Scala API also uses this Option to design it’s API in very clean way.
In Scala, Option type is not a Collection. It is a container to have maximum of one value.

Option type contains either one value or zero value. If it contains one value that is Some type. If it contains zero value, that is None type.
In Scala API, Option type has two sub-types:
  • Some
  • None
Some:- 
In Scala, Some is a case class. Some is used to represent the existent of a value.
None:- 
In Scala, None is a case object. None is used to represent the non-existent of a value.


Scala’s Option Source Code

In Scala Language, Option/Some/None all are defined in “scala” package as shown below:
Option.scala
package scala

sealed abstract class Option[+A]
final case class Some[+A](x: A) extends Option[A]
case object None extends Option[Nothing]
NOTE:- Option is a Sealed abstract class. We will discuss about “What is Sealed and what is the use of it” in my coming posts.

Advantages of Option

In Scala, the main advantage of Option/Some/None Pattern is:
  • Easy to use in Pattern Matching
  • To avoid null checks
  • To avoid NullPointerExcepiton
  • To avoid boiler-plate code in null checks and NullPointerExcepiton Handling
NOTE:- Before Java SE 8, we need to write lot of boiler-plate code to check nulls and NullPointerExcepiton Handling.
NOTE:-Like Scala’s Option, Java SE 8 has java.util.Optional class to do the same thing.

Basic Examples of Option

Here we will discuss some Option Basics using Scala REPL.
Example-1:-
We can use Option[T] type to indicate a value is present using Some[T] or not-present using None. Please refer this example where T is String.
scala> val empname:Option[String] = Option("Rams")
empname: Option[String] = Some(Rams)

scala> val empname:Option[String] = Some("Rams")
empname: Option[String] = Some(Rams)

scala> val empname:Option[String] = None
empname: Option[String] = None
Example-2:-
We can use get method to retrieve a value of Option type as shown below.
scala> val empname:Option[String] = Some("Rams")
empname: Option[String] = Some(Rams)

scala> empname
res1: Option[String] = Some(Rams)

scala> empname.get
res2: String = Rams
NOTE:- However, it’s NOT recommended to use get method to value from an Option type. Because sometimes it may have a value and sometime it may not have a value. See next example.
Example-3:-
Sometimes, Option type’s get method may throw NoSuchElementException as shown below.
scala> val empname:Option[String] = None
empname: Option[String] = None

scala> empname.get
java.util.NoSuchElementException: None.get
  at scala.None$.get(Option.scala:347)
  at scala.None$.get(Option.scala:345)
  ... 33 elided
So it’s not recommended to use get method to retrieve value of Option type. Then how to solve this problem. See my next example.

Scala Option Type’s getOrElse Method

In Scala, Option type has a method “getOrElse” to avoid that NoSuchElementException issues. It is defined as shown below:
final def getOrElse[B >: A](default:  B): B
This getOrElse method returns a value form Option if it contains otherwise it returns default value (Which is defined as a parameter). Let us explore this method with some examples below.
Example-4:-
How to use getOrElse method to retrieve a value from Option safely.
If Option type has a value, it returns that value.
scala> val empname:Option[String] = Some("Rams")
empname: Option[String] = Some(Rams)

scala> empname.getOrElse("No Name")
res4: String = Rams
If Option type does NOT have a value, it returns the default value.
scala> val empname:Option[String] = None
empname: Option[String] = None

scala> empname.getOrElse("No Name")
res5: String = No Name

Real-time Scenarios of Option

Apart from learning Scala Concepts, we should understand how to use each and every small concept in a real-time Scala based projects.

I have delivered another post on this concept. Please click here: Option Real-time Scenarios 
Scala's Option Type In Pattern Matching
One of the major advantages of Scala's Option type is that easy to write Pattern Matching on it.  I have delivered another post on this concept. Please click here: Option In Pattern Matching 


NOTE:- If you are new to Scala’s Pattern Matching. No worries. I’m going to write 2 to 3 posts on this concept soon.
That’s it all about “Scala Option” type some basics. We will discuss some more important Scala Option type concepts in my coming posts.
Please drop me a comment if you like my post or have any issues/suggestions.
I love your valuable comments so much.

No comments:

Post a Comment