Updated April 11, 2023
Introduction to Scala Option
Scala Option is used in Scala Object-oriented programming whenever the return type of function can be a null. It has the values of a type or none in it, so the method returns an instance of option with the values whether be it any values or none. So in Scala Option if that value is returned, it is given back with some class generated, and if no values are returned the none class is generated. Some and none called to be called as the children of option class.
Syntax and Parameters:
classOption [+A] extends Product with Serializable
Option has the type parameterized. We can define an option and give the data type whatever we want to for that.
Option[String], Option[Int], Option[File] etc.
The value could be either some or none class.
Working and Examples
Scala Option checks the return type of the method and returns the value with some class if there is a value returned or else none is returned for a null value. Whenever a null is returned over a normal function and when it is not handled with in the program we are most liable to get an error of NULLpointerException, but with the help of the option over the scala code we will not get this Null pointer Exception. It is better used with Match function.
Let us check the methods and ways to create Scala Option with examples:
Example #1
Code:
object Main extends App {
val a = Map("Arpit" -> "Banglaore","Anand" -> "Developer")
val b = a.get("Arpit")
val c = a.get("Aman")
println(b)
println(c)
}
Output:
This some and none are part of Scala Option that searches for the key and returns the value if it founds that and none is returned if it doesn’t find the value. So here it uses the Scala Option to fetch the value and handle none.
Example #2
Code:
object Main extends App {
val e = Map(1 ->"A" , 2 -> "b" , 3 -> "c")
val b = e.get(1)
val c = e.get(4)
println(b)
println(c)
}
Output:
It can be best use when we have to implement the Scala Pattern Matching.
Let us check how we use that with the help of an example:
Example #3
Pattern matching matches the values over the collection whatever it is if the particular thing is found it returns the value else nothing is returned. So we will check how this is achieved with the help of Options.
Code:
object Main extends App {
def matc(a : Option[String]) = a match
{
case Some(v) => ("This the the Returned Value "+v)
case None => ("Nothing Found")
}
val f = Map("Arpit" -> "Bangalore" ,"Anand" -> "Pune")
val b = matc(f.get("Arpit")
)
val c = matc(f.get("Aa"))
println(b)
println(c)
}
Output:
We made a function named as matc that takes up the Option as an input and then checks whether the element is found or not, if the element is found we will get the value and if not the nothing return string is returned. A map is made with the key and value. Then this matches the value and we get the String written inside as the result and if nothing is there none is returned.
Methods
Given below are the methods that can be used with Scala Option:
1. isEmpty
It checks whether the result we got from the option is Empty or Not. It returns a Boolean Value.
Example:
Code:
object Main extends App {
def matc(a : Option[String]) = a match
{
case Some(v) => ("This the the Returned Value "+v)
case None => ("Nothing Found")
}
val f = Map("Arpit" -> "Bangalore" ,"Anand" -> "Pune")
val res20 = matc(f.get("Arpit")
)
val res19 = matc(f.get("Aa"))
val d = res20.isEmpty
val e = res19.isEmpty
println(res20)
println(res19)
println(d)
println(e)
}
Output:
2. Non Empty
Returns True if it is Non Empty.
Example:
Code:
object Main extends App {
def matc(a : Option[String]) = a match
{
case Some(v) => ("This the the Returned Value "+v)
case None => ("Nothing Found")
}
val f = Map("Arpit" -> "Bangalore" ,"Anand" -> "Pune")
val res20 = matc(f.get("Arpit")
)
val res19 = matc(f.get("Aa"))
val d = res20.nonEmpty
val e = res19.nonEmpty
println(res20)
println(res19)
println(d)
println(e)
}
Output:
3. Zip And Unzip
Zips the value of the option it gets and makes a paired value, whereas unzip helps in unzipping the values from each other.
Example:
Code:
object Main extends App {
def matc(a : Option[String]) = a match
{
case Some(v) => ("This the the Returned Value "+v)
case None => ("Nothing Found")
}
val f = Map("Arpit" -> "Bangalore" ,"Anand" -> "Pune")
val res20 = matc(f.get("Arpit")
)
val res19 = matc(f.get("Aa"))
val c = res19 zip res20
val e = c.unzip
println(c)
println(e)
}
Output:
4. getOrElse
It is just like an if else method, this works like if the option has some values get that or else return the default value.
Example:
We are creating a variable of type Option with some and none values inside it.
Code:
object Main extends App {
val a : Option[Int] = Some(5)
val b : Option[Int] = None
val c : Option[String] = Some("Arpit")
println(a.getOrElse(10))
println(a.getOrElse(3))
println(b.getOrElse(0))
println(b.getOrElse(3))
println(b.getOrElse(4))
println(c.getOrElse("Annad"))
println(c.getOrElse(4))
println(c.getOrElse(4.0))
}
Output:
Here a, b, c are three variables of type Option having the type inside as String and Int.
The get or else method will check if it has some value and return accordingly.
Conclusion
From the above article we saw how Scala Option works and the benefit of using Scala Option over the Object oriented programming. With the help of examples we saw how Scala option handles the null values and the various results associated with it. We checked the syntax and functioning, so Scala Option is a better and a good approach for Scala Object-Oriented Programming.
Recommended Articles
We hope that this EDUCBA information on “Scala Option” was beneficial to you. You can view EDUCBA’s recommended articles for more information.