Scala pattern matching for case class

We learned about pattern matching in the past and there were a lot of “we will learn about this later“.

Some of thelateris today !

One of the big advantage ofcase classoverclassis the built-in support for pattern matching. We are going to see the same advantage withcase objectin a later SKB.

Coming back tocase class. You can perform several test using pattern matching:

  • Extraction of field:case Person(firstName, lastName) => ???
  • Extraction of field with condition:case Person(firstName, lastName) if firstName.startsWith("L") => ???
  • Filter with exact match:case Person("Leo", lastName) => ???
  • Filter with exact match and handle on case class:case p @ Person("Leo", lastName) => ???
  • Several filters:case p @ (Person("a", _) | Person(_, "b")) => ???
Reveal more information and clues
Load Exercise