5. Option, and life without null

Someone rings the restaurant to say they’re running late and gives a surname. When the staff search for the booking, there might not be one under that name. Java code might return null from that search, or an Optional if its author was being careful. In Scala the search returns an Option. A field or a parameter whose value might be missing is an Option as well, so you’ll hardly ever see null written in Scala.

5.1. Looking up a booking

Add a method to the end of desk.scala that searches a list of bookings by name:

def findBooking(name: String, bookings: List[Booking]): Option[Booking] =
  bookings.find(_.party.name == name)

A list’s find returns an Option[Booking], a value that holds a booking or nothing. findBooking passes it straight on. To try it from the command line, add a third @main to the end of main.scala, one that takes the name as its parameter:

@main def lookUp(name: String): Unit =
  val booking = findBooking(name, tonight)
  println(label(booking))
$ scala compile .
[error] ./main.scala:33:17
[error] Found:    (booking : Option[fullybooked.Booking])
[error] Required: fullybooked.Booking
[error]   println(label(booking))
[error]                 ^^^^^^^
Compilation failed

We’ve given label an Option[Booking] where it wants a Booking. Had findBooking returned a Booking that could be null, this version would have compiled, printed the Patel booking and crashed on the null it got for Smith. With an Option, the compiler won’t let us reach the booking until we’ve said what happens when there isn’t one. Replace lookUp with a version that handles both cases:

@main def lookUp(name: String): Unit =
  findBooking(name, tonight) match
    case Some(booking) => println(label(booking))
    case None          => println(s"No booking under $name")

An Option is either a Some holding a value or None. Some is a case class with one field, so Some(booking) is a pattern like the ones that took a Booking apart in chapter 3. It matches when there’s a value and gives that value the name booking. The None case matches when there’s no value. Leave either case out and the compiler warns that the match is missing one.

$ scala run . --main-class fullybooked.lookUp -- Patel
19:30 table 1: Patel, party of 2
$ scala run . --main-class fullybooked.lookUp -- Smith
No booking under Smith

An Option also has a get method which, like Optional.get in Java, returns the value or throws a NoSuchElementException when there’s none. Calling get on the Smith search would crash lookUp as a null would have. I don’t use get and I ask for it to be changed when I see it in a review.

5.2. A field that might be empty

The staff would like to ring a party that’s late, but not every party leaves a number. In Java this is often a field that may be null: the documentation for Optional says it’s mainly intended for return types. Scala code uses Option for a field that might be empty as well. Change the first line of Party in model.scala:

case class Party(name: String, size: Int, phone: Option[String] = None):

The default means every Party("Nakamura", 2) we’ve already written still compiles. The two patterns in label don’t, because they list the fields of Party by position. With a third field added, each pattern needs a _ to skip it:

def label(booking: Booking): String = booking match
  case Booking(Party(name, size, _), Table(number, _), time, Status.Confirmed) if size >= 6 =>
    s"$time table $number: $name, party of $size (deposit taken)"
  case Booking(Party(name, size, _), Table(number, _), time, _) =>
    s"$time table $number: $name, party of $size"

Whenever you add a field to a case class, the project stops compiling at every pattern that lists that class’s fields.

A party with a number gets it wrapped in Some. In tonight.scala, replace the four bookings from Okonkwo to Moreau with these:

  Booking(Party("Okonkwo", 5, Some("07700 900141")), Table(5, 6),
    LocalTime.of(19, 30), Status.Confirmed),
  Booking(Party("Patel", 2, Some("07700 900129")), Table(1, 2),
    LocalTime.of(19, 30)),
  Booking(Party("Lindqvist", 7, Some("07700 900152")), Table(6, 8),
    LocalTime.of(20, 0), Status.Confirmed),
  Booking(Party("Moreau", 3, Some("07700 900164")), Table(3, 4),
    LocalTime.of(20, 15), Status.Confirmed),

Start the REPL with scala repl ., import fullybooked.* and java.time.LocalTime again, and ask about the Moreau party:

scala> findBooking("Moreau", tonight).map(_.party.size)
val res0: Option[Int] = Some(3)

scala> findBooking("Moreau", tonight).map(_.party.phone)
val res1: Option[Option[String]] = Some(Some("07700 900164"))

scala> findBooking("Moreau", tonight).flatMap(_.party.phone)
val res2: Option[String] = Some("07700 900164")

scala> findBooking("Abara", tonight).flatMap(_.party.phone)
val res3: Option[String] = None

scala> findBooking("Smith", tonight).flatMap(_.party.phone)
val res4: Option[String] = None

On an Option, map applies the lambda to the value if there is one and gives back the result in a Some. It leaves a None as it is. In the second line the lambda returns an Option of its own, the phone number, so the result is an Option inside an Option. flatMap removes the extra layer, much as it joined inner lists into one list. Use it whenever the next step might also come back empty.

The last two lines both give None: the Abaras have a booking and left no number, while nobody called Smith has a booking at all.

Replace lookUp once more, so that it prints the number:

@main def lookUp(name: String): Unit =
  findBooking(name, tonight) match
    case Some(booking) =>
      val phone = booking.party.phone.getOrElse("none given")
      println(label(booking))
      println(s"Phone: $phone")
    case None =>
      println(s"No booking under $name")

Here getOrElse returns the value if there is one and its argument if there isn’t.

$ scala run . --main-class fullybooked.lookUp -- Moreau
20:15 table 3: Moreau, party of 3
Phone: 07700 900164
$ scala run . --main-class fullybooked.lookUp -- Abara
18:45 table 4: Abara, party of 4
Phone: none given

5.3. Options and lists

An Option behaves like a collection that holds one value or none. It has most of the methods a list has and can be mixed with lists:

scala> tonight.map(_.party.phone)
val res0: List[Option[String]] = List(
  None,
  None,
  None,
  Some("07700 900141"),
  Some("07700 900129"),
  Some("07700 900152"),
  Some("07700 900164"),
  None
)

scala> tonight.flatMap(_.party.phone)
val res1: List[String] = List("07700 900141", "07700 900129", "07700 900152", "07700 900164")

scala> findBooking("Moreau", tonight).exists(_.party.size > 2)
val res2: Boolean = true

scala> findBooking("Haddad", tonight).filter(holdsTable)
val res3: Option[Booking] = None

Mapping tonight’s bookings to their phone numbers gives a list of options. flatMap treats each Option as a list of one or nothing and joins them up, which leaves the four numbers we have. That makes flatMap the method for a list where only some of the elements have the value you want. On a single Option, exists asks whether there’s a value that passes the test, and filter turns a Some into a None when its value fails. The Haddads have a booking, but it’s cancelled and doesn’t hold a table.

I don’t wrap a List in an Option, because an empty list already says there’s nothing. With Option[List[Booking]], callers would have two kinds of nothing to check for.

Sometimes two parties ask to swap tables. The staff can only do it if both bookings are found. Add this to the end of desk.scala:

def swapTables(
    first: String,
    second: String,
    bookings: List[Booking]
): Option[(Booking, Booking)] =
  for
    a <- findBooking(first, bookings)
    b <- findBooking(second, bookings)
  yield (a.copy(table = b.table), b.copy(table = a.table))

A for with yield works on options too. Its clauses usually go on separate lines beneath the for, as here, and then they need no semicolon between them. Each <- takes the booking out of an Option. When either search comes back as None, the yield doesn’t run and the whole result is None. Otherwise the result is a Some holding a tuple of two copies, each with the other’s table. Restart the REPL to pick up the new method and repeat the two imports:

scala> swapTables("Moreau", "Abara", tonight).map((a, b) => (a.table.number, b.table.number))
val res0: Option[(Int, Int)] = Some((4, 3))

scala> swapTables("Moreau", "Smith", tonight)
val res1: Option[(Booking, Booking)] = None

5.4. Asking for one element

Our reserve still has a minBy that throws on an empty list and an isEmpty check to protect it. The library has a second version of nearly every method that can throw like that, one that returns an Option:

scala> "19".toIntOption
val res0: Option[Int] = Some(19)

scala> "seven".toIntOption
val res1: Option[Int] = None

scala> tonight.headOption.map(_.party.name)
val res2: Option[String] = Some("Haddad")

scala> List[Booking]().headOption
val res3: Option[Booking] = None

scala> val covers = Map(18 -> 6, 19 -> 5)
val covers: Map[Int, Int] = Map(18 -> 6, 19 -> 5)

scala> covers.get(19)
val res4: Option[Int] = Some(5)

scala> covers.get(22)
val res5: Option[Int] = None

The versions that throw are toInt on the string, head on the list and the plain covers(22) lookup. The names are mostly regular: head, last, max, min, maxBy and minBy all gain an Option version by adding the word to the end. So do the string conversions, where toIntOption saves catching the NumberFormatException that Java’s Integer.parseInt throws. Map is the odd one out, with a plain get.

With minByOption, reserve can lose its isEmpty check. Add a method to availability.scala, above reserve, that picks a table without booking it:

def bestTable(party: Party, time: LocalTime, bookings: List[Booking]): Option[Table] =
  freeAt(time, bookings)
    .filter(_.seats >= party.size)
    .minByOption(_.seats)

If nothing is free, or nothing free is big enough, the answer is None. Now replace reserve with a version built on bestTable:

def reserve(store: BookingStore, party: Party, time: LocalTime): Option[Booking] =
  bestTable(party, time, store.all) match
    case None => None
    case Some(table) =>
      Thread.sleep(300)
      val booking = Booking(party, table, time, Status.Confirmed)
      store.add(booking)
      Some(booking)

This reserve returns the booking it made, if it made one, and leaves the wording to its caller. The old version returned a String for the customer. A match that turns None into None and a Some into another Some is map written out by hand. I’ve kept the match because the second case sleeps and saves before it builds its value, which takes several lines. Chapter 7 shows how to write a lambda that long.

The project won’t compile yet, because lastTable still expects a String from reserve. First add the wording to the end of desk.scala:

def reply(name: String, result: Option[Booking]): String =
  result
    .map(booking => s"$name: table ${booking.table.number} at ${booking.time}")
    .getOrElse(s"$name: sorry, nothing free")

In reply, map changes the value if it’s there and getOrElse says what to use if it isn’t. A match with two cases would do the same job. So would fold, which takes the fallback and the lambda in one call. I find map and getOrElse easier to read than fold.

Then, in lastTable, replace everything from var toIbrahim down to the two printlns that follow the joins:

  var toIbrahim: Option[Booking] = None
  var toCastellanos: Option[Booking] = None
  val first = Thread(() => toIbrahim = reserve(store, Party("Ibrahim", 7), eight))
  val second = Thread(() => toCastellanos = reserve(store, Party("Castellanos", 8), eight))
  first.start()
  Thread.sleep(100)
  second.start()
  first.join()
  second.join()

  println(reply("Ibrahim", toIbrahim))
  println(reply("Castellanos", toCastellanos))

The two vars start as None and need their type written out, because None alone doesn’t tell the compiler what they’ll hold later. Run lastTable and it prints the same four lines as in chapter 4, with the double booking still there.

Restart the REPL, repeat the imports and try bestTable on tonight’s bookings:

scala> val eight = LocalTime.of(20, 0)
val eight: LocalTime = 20:00

scala> bestTable(Party("Silva", 2), eight, tonight)
val res0: Option[Table] = Some(Table(number = 1, seats = 2))

scala> bestTable(Party("Achterberg", 5), eight, tonight)
val res1: Option[Table] = None

scala> bestTable(Party("Zielinski", 9), eight, tonight)
val res2: Option[Table] = None

The Silvas get table 1 and the other two parties get None. For the party of five, the tables that would seat them are taken at eight. For the party of nine, no table in the restaurant is big enough at any time. The staff would say different things to those two callers, and None can’t tell them which is which. Use an Option when nobody will ask why the value is missing. Chapter 9 covers the case where the caller needs to know.

5.5. Nulls from Java

Scala has null because it runs on the JVM and calls Java. Scala’s own library almost never returns null, but Java methods do. This session doesn’t use the project, so start the REPL with plain scala repl:

scala> val port = System.getenv("FULLY_BOOKED_PORT")
val port: String = null

scala> Option(port)
val res0: Option[String] = None

scala> Option("8080")
val res1: Option[String] = Some("8080")

scala> Some(port)
val res2: Some[String] = Some(null)

scala> sys.env.get("FULLY_BOOKED_PORT")
val res3: Option[String] = None

scala> sys.env.get("FULLY_BOOKED_PORT").flatMap(_.toIntOption).getOrElse(8080)
val res4: Int = 8080

Wrapping a value in Option(...) turns null into None. Some(...) doesn’t check, so Some(port) came out as Some(null). If a Java method’s documentation says it can return null, put the call in Option(...). System.getenv is one of those methods, returning null for a variable that isn’t set. Left unwrapped, port would throw a NullPointerException at a call such as port.length.

The environment is also available as a Scala Map called sys.env, where get gives an Option directly. The last line reads a setting, parses it and falls back to 8080, whether the variable is missing or holds something that isn’t a number.

The compiler can check for nulls too, if you give it the option -Yexplicit-nulls. null then stops being a valid String in Scala code. A nullable value has to be declared as String | Null. Types written with that |, which chapter 2 read as “either”, are called union types. The check doesn’t cover what a Java method returns, so those values still need wrapping in Option(...). The option is off by default and the documentation lists it as experimental.

Java methods that return Optional need one import:

scala> import scala.jdk.OptionConverters.*

scala> val firstBig = java.util.List.of(4, 2, 8).stream().filter(_ > 5).findFirst()
val firstBig: java.util.Optional[Int] = Optional[8]

scala> firstBig.toScala
val res0: Option[Int] = Some(8)

scala> Option("8080").toJava
val res1: java.util.Optional[String] = Optional[8080]

The import brings in toScala, which turns an Optional into an Option, and toJava, which goes the other way. Convert where the Java value arrives and use Option everywhere after that.

5.6. Exercises

Solutions are in the book’s downloadable code, under ch05/exercises.

  1. Write largestParty(bookings: List[Booking]): Option[Booking], the biggest party among the bookings that hold a table. Try it on tonight and on List().

  2. The staff type a table number. Write tableNumbered(input: String): Option[Table], which gives None both for text that isn’t a number and for a number that isn’t one of the six tables.

  3. Write nextArrival(now: LocalTime, bookings: List[Booking]): Option[Booking], the next party due at or after now among the bookings that hold a table.

  4. Callers don’t always get the capitals right. Write a version of findBooking that tries an exact match first and then one that ignores case. orElse takes a second Option to use when the first is None, and Java’s equalsIgnoreCase works on a Scala String.

  5. get on a java.util.HashMap returns null for a missing key. Given a java.util.Map[String, String] of allergy notes keyed by party name, write allergy(name: String, notes: java.util.Map[String, String]): Option[String].

  6. Predict each result, then try it in the REPL: Some(4).map(_ * 2), Some(4).filter(_ > 5), Some(Some(4)).flatten and List(Some(1), None, Some(3)).flatten. flatten removes one layer of nesting, which is the second half of what flatMap does.