Sum ADTs

Sum algebraic data types are the ones in which we can simply enumerate all the possible values of a type and provide a separate constructor for each value. As an example, let's consider the months of the year. There are only 12 and they cannot change (hopefully):

sealed abstract trait Monthcase object January extends Monthcase object February extends Monthcase object March extends Monthcase object April extends Monthcase object May extends Monthcase object June extends Monthcase object July extends Monthcase object August extends Monthcase object September extends Monthcase object October extends Monthcase object November extends Monthcase object December extends Monthobject MonthDemo {  def main(args: Array[String]): Unit = { val ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.