Data modelling and pattern matching

A program that models its data well is shorter everywhere else. This part is about the values a Scala program is built from: a tuple for two things that belong together, a case class for a record, an enum for a choice, and a sealed hierarchy for a choice whose cases each carry their own data. The compiler knows every case of such a value, so a match over it can be proved to be complete.

The part then takes the match apart: guards, alternatives, typed patterns and bindings, the extractors that make Name(first, last) work, and the three types that say a value may be absent or an operation may fail — Option, Either and Try.

033 Tuples a pair and a triple, _1 and swap, destructuring, and the Tuple types Open 034 Case classes the equals, toString and copy the compiler writes, and a nested value Open 035 Enums an enum with parameters and methods, a case with fields, and values Open 036 Sealed hierarchies a sealed trait, an exhaustive match, and the warning for a missing case Open 037 Pattern matching match, guards, alternatives, a typed pattern, and a binding with @ Open 038 Extractors unapply and unapplySeq, a custom extractor, and the Name(...) notation Open 039 Option, Either and Try the three ways to say that something may fail, and a for over each of them Open

Lessons