Home > Articles

This chapter is from the book

4.7 The Option Type

The Option class in the standard library expresses values that might or might not be present. The subclass Some wraps a value. The object None indicates that there is no value.

var friend: Option[String] = Some("Fred")
friend = None // No friend

This is less ambiguous than using an empty string and safer than using null for a missing value.

Option is a generic type. For example, Some("Fred") is an Option[String].

The get method of the Map class returns an Option. If there is no value for a given key, get returns None. Otherwise, it wraps the value inside Some.

val scores = Map("Alice" -> 10, "Bob" -> 7, "Cindy" -> 8)
val alicesScore = scores.get("Alice") // Some(10)
val dansScore = scores.get("Dan") // None

To find out what is inside an Option instance, you can use the isEmpty and get methods:

if alicesScore.isEmpty
  then println("No score")
  else println(alicesScore.get)

That’s tedious, though. It is better to use the getOrElse method:

println(alicesScore.getOrElse("No score"))

If alicesScore is None, then getOrElse returns "No score".

A more powerful way of working with options is to consider them as collections that have zero or one element. You can visit the element with a for loop:

for score <- alicesScore do println(score)

If alicesScore is None, nothing happens. If it is a Some, then the loop executes once, with score bound to the contents of the option.

You can also use methods such as map, filter, or foreach. For example,

val biggerScore = alicesScore.map(_ + 1) // Some(score + 1) or None
val acceptableScore = alicesScore.filter(_ > 5) // Some(score) if score > 5 or None
alicesScore.foreach(println) // Prints the score if it exists

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.