Product ADTs

In product algebraic data types, we cannot enumerate all the possible values. There are usually too many to manually write them. We cannot provide a separate constructor for each separate value.

Let's think about colors. There are different color models, but one of the most famous ones is RGB. It combines the different values of the main colors (red, green, and blue) in order to represent other colors. If we say that each of these colors can have a value between 0 and 255, this would mean that to represent all possibilities, we would need to have 2563 different constructors. That's why we can use a product ADT:

sealed case class RGB(red: Int, green: Int, blue: Int)

object RGBDemo {
def main(args: Array[String]): Unit = {
val magenta = RGB(255, 0, 255)
System.out.println(s"Magenta in RGB is: $magenta")
}
}
Now we can see that for the product ADTs, we have one constructor for different values.