Reading from an Option

Now we have an Option and need to take the value out of it. The most obvious way to do this is in a "null-checking" style:

if (opt1.isDefined) println(opt1.get)
if (opt1.isEmpty) println("Ooops, option is empty") else println(opt1.get)

Here, we're using one of two emptiness checks and in this case, if an Option is non-empty, we call .get to retrieve its value. Besides being quite verbose, the main disadvantage of this approach is that it is easy to forget to check if an option has been defined. If None.get gets called, it will throw a NoSuchElementException:

scala> None.get
java.util.NoSuchElementException: None.get
at scala.None$.get(Option.scala:378)
... 40 elided

There are a few more methods that allow you to check whether the contents of the option satisfy a given criteria, but they all suffer in the same way:

if (option.contains("boo")) println("non-empty and contains 'boo'")
if (option.exists(_ > 10)) println("non-empty and greater then 10")
if (option.forall(_ > 10)) println("empty or greater then 10")

The contains method compares its argument with the contents of the option if it has been defined. exists takes a predicate that is applied to the value of an option if it is non-empty. Compared to the other methods, forall is special because it returns true if a predicate applied to the argument holds for a non-empty option or if an option is empty. 

Another way to get a value out of Option is to deconstruct it:

if (opt.isDefined) { val Some(value) = opt }

You can also use pattern matching to avoid checking for non-emptiness completely:

opt match {
case Some(value) => println(value)
case None => println("there is nothing inside")
}

Sometimes, all that is needed for the caller is a "default" value if an Option is empty. There is a special method for this called getOrElse:

val resultOrDefault = opt.getOrElse("No value")

Another similar method is orNull. It is not very useful in Scala code, but is very convenient for Java interoperability and is available for Option[AnyRef]. It returns null in the case of an empty option or the option's value otherwise:

scala> None.orNull
res8: Null = null

The foreach method feels quite different from what we've seen so far as it executes a function on a value of an Option in the case it is defined:

scala> val opt2 = Some("I'm a non-empty option")
opt2: Some[String] = Some(I'm a non-empty option)
scala> opt2.foreach(println)
I'm a non-empty option

The reason why it is special compared to the other methods we've seen so far is that it does not treat the option as a special value. Instead, it is semantically formulated as a callback – "if this effect has (had) place, execute the following code on it."

This view of an Option offers another possibility so that we can work with it – to provide higher order functions that will be executed in the case of an empty or non-empty option. Let's see how this works in detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.213.212