uh_big_mike_boi
uh_big_mike_boi

Reputation: 3470

What type of algebraic data type is Option[T] or Optional<T>

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

Answers (1)

castletheperson
castletheperson

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:

  1. None
  2. 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

Related Questions