Sum or Product Type?

2.8k views Asked by At

Given the following Algebraic Data Type:

scala> sealed trait Person
defined trait Person

scala> case class Boy(name: String, age: Int, x: String) extends Person
defined class Boy

scala> case class Girl(name: String, age: Int, y: Boolean) extends Person
defined class Girl

Note - I know that it's not a Recursive Type - there's no recursion involved.

So, is this a Sum or Product Type? Why?

1

There are 1 answers

0
Minh Thai On

In this case, Person can be considered as a sum type since an instance of it is either Boy or Girl.

Boy (or Girl) is a product type since an instance of Boy is a combination of type String, Int, and String.

Here is a very nice article about this "hybrid" case Algebraic Data Types in Scala