Scala Option pattern matching

Easy one today.

We are going to see how the concepts ofunapply,pattern matchingandOptionwork together.

Under the hoodOptionis asealed traitthat we saw before. It has two implementations:SomeandNone.

TheSomehas anunapplymethod that let you extract the value out. You can see it being used incase Some(n) =>. TheNonedo not since it defines the case when theOptionis empty.

On thepattern matchingsides of things, because it is asealed trait, the compiler knows how many possible cases there are: 2. If you remove theSomecase or theNoneone, the compiler will give you a warning related to the completeness of the match cases.

Reveal more information and clues
Load Exercise