Reputation: 3470
So it's my understanding there are two types of Algebraic Data Types (ADTs). For the case of Option[T]
in Scala or Optional<T>
in Java, would this be an example of a sum type or a product type?
Upvotes: 1
Views: 452
Reputation: 33486
Defining the Option[T]
type in Haskell makes it clear that it's a sum type.
data Option t = None | Some t
Values of type Option t
can be one of 2 things:
None
Some t
So Option[T]
and Optional<T>
both take a type T
and then add 1 more possible value (None
).
For fun, we can also translate this ADT into an algebraic equation:
Option(t) = 1 + t
To see why, see this question: Abusing the algebra of algebraic data types - why does this work?
Upvotes: 2