The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

kmeredith (23) [Avatar] Offline
#1
Chapter 2 notes:

Another big advantage with pattern matching in Scala is that the compiler checks for the
exhaustiveness of the pattern match. If you forgot to insert any of the enumerations of
Balance in the pattern match clause, the compiler will issue a warning.


Perhaps it's implied (or expected to an intermediate Scala user), but I think it'd be helpful to note that the sealed keyword is responsible for exhaustiveness.

scala> trait Parent  // not exhaustive
defined trait Parent

scala> case object Boy extends Parent
defined object Boy

scala> case object Girl extends Parent
defined object Girl

scala> def f(p: Parent): Boolean = p match {
     |   case Boy => true
     | }
f: (p: Parent)Boolean
kmeredith (23) [Avatar] Offline
#2
My mistake, I spoke too soon.

Ch 3 explains sealed traits.

I'll make sure to read the whole book before making comments of this type.