Scala Option pattern matching

Easy one today.

We are going to see how the concepts of unapply, pattern matching and Option work together.

Under the hood Option is a sealed trait that we saw before. It has two implementations: Some and None.

The Some has an unapply method that let you extract the value out. You can see it being used in case Some(n) =>. The None do not since it defines the case when the Option is empty.

On the pattern matching sides of things, because it is a sealed trait, the compiler knows how many possible cases there are: 2. If you remove the Some case or the None one, the compiler will give you a warning related to the completeness of the match cases.

Reveal more information and clues
Load Exercise