I code reviewed the Option collection in Scala.

Let’s create a new Option that contains a String.

var indeed: String = "Indeed"

var optionIndeed = Option(indeed)

Options have two subtypes “Some” and “None”. The factory creates ‘Some(x)’ if the argument is not null, and ‘None’ if it is null.

To get the value simply call:

optionIndeed. getOrElse ("Empty") "Empty" is the default value.

There are few ways to safely extract the value from an Option

Figure 1: Other ways to get the value out of an Option

The ‘getOrElse(Default)’ method goes into the Scala class hierarchy (Figure 2) to find the common ancestor for the value of the Option and the Default.

e.g:

Option[Int] and Default=String.

getOrElse(Default) will return Scala.Any

A = Int

B = Scala.Any

if (isEmpty) default else this.get final def getOrElse[B >: A] (default: => B): B =if (isEmpty) default else this.get The “>:” is called the “Lower Bound Operator” it restricts type B to those that are equal to or extended by the type A. In our case “default” is transformed to scala.Any because it is extended by Int. The anonymous function, “=>”, which separates the function’s argument list from its body takes in nothing and returns type “B”.

Figure 2: Scala class hierarchy

The complete code:

I am in the process of learning Scala from Scratch for a project at Expedia. We are using the Actor pattern to efficiently shop multiple hotel inventory services.

To get started with the Actor Pattern read these articles in order. Ignore everything else on the internet. There was so many abstract articles without code about The Reactive Manifesto.