Scala Options for Dummies, part 2

You can watch the video version of this post or read the text version below. But you shouldn’t need to do both! Pick whichever one you prefer!

In part 1 of the series, we learned that an Option is a class in Scala that can wrap a value and represent the existence of the value as a “Some” (the value is there) or a “None” (the value is not there). This helps alleviate some of the problems of dealing with null values in code.

To reiterate the main point from part 1 another way, an Option can be Some or None, and you can pass a hard-coded value or a variable to an Option:

val hello: String = " Hello World "
val nil: String = null

// Make an Option with Option()
val optionNil = Option(nil) //res0: Option[String] = None
val optionHi = Option(hello) //res1: Option[String] = Some( Hello World )

In this snippet we’ve created two variables holding Strings. The variable “hello” holds ” Hello World ” and the variable “nil” holds null. Next we create the Options of those variables: “optionNil” and “optionHi.” As you can see the result is “None” for the variable holding null and “Some” for the variable that has the value ” Hello World “.

You can pattern match on an Option. Here are some quick examples.

// boolean existence 
optionNil match {
  case Some(s) => true
  case None => false

// other existence
optionHi match {
  case Some(s) => "Something was there"
  case None => "Nothing was there"

// Return a value
val optionHiValue: String = optionHi match {
  case Some(s) => s
  case None => "Nothing was there"

val optionNilValue: String = optionNil match {
  case Some(s) => s
  case None => "Nothing was there"

As you can see, you can pattern match Options holding “Some” with a “case Some(…)”. I want to point out that we have created the variable “s” in each of these cases, but you could just as well use any arbitrary name or in half of these cases (where in the case you aren’t actually going to refer back to the value contained in the Some) you could have used the underscore (e.g. “case Some(_) => …”).

I like to always show a practical example of usage so let’s try using the pattern matching we learned above to define a function that returns the length of the value not including spaces:

def lengthWithoutSpaces(opt: Option[String]): Int = opt match {
  case Some(s) => s.replace(" ", "").length
  case None => 0

The “lengthWithoutSpaces” function takes an Option we call “opt” and matches it against case Some(s) or case None. If it is a Some, we take the value inside the Some (referred to as the variable “s”) and replace all spaces with nothing and then get the resulting string length.

In the “None” case, we decided to return “0” for the length. That makes sense. The length of None is 0 after all!

Now we can do something like the below (an example of Scala’s “String Interpolation”):

println(s"The length of optionHi is ${lengthWithoutSpaces(optionHi)}.")
println(s"The length of optionNil is ${lengthWithoutSpaces(optio

The result will be:

The length of optionHi is 10.
The length of optionNil is 0.

Please leave a comment if you have any questions or feedback!

Leave a Reply

Your email address will not be published. Required fields are marked *