Algebraic data types and class hierarchies

Algebraic data types and class hierarchies are other unifications in the Scala programming language. In other functional languages, there are special ways to create custom algebraic data types. In Scala, this is achieved using class hierarchies and namely case classes and objects. Let's see what an ADT actually is, what types there are, and how to define them.

ADTs

Algebraic data types are just composite types that combine other existing types or just represent some new ones. They have only data and do not contain any functionality on top of this data as normal classes would. Some examples can include the day of the week or a class that represents an RGB color—they have no extra functionality and they just ...

Get Scala Design Patterns 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.