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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With