Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum or Product Type?

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?

like image 824
Kevin Meredith Avatar asked Nov 01 '22 02:11

Kevin Meredith


1 Answers

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

like image 55
Minh Thai Avatar answered Nov 15 '22 05:11

Minh Thai