Sum type
From Coderwiki
More actions
A sum type is a type of algebraic data type. It is a user-defined data type that stores a fixed set of possible values. In other words, it is a choice of possibilities as to the data it contains.
Example
edit edit sourceSuppose we are in a zoo and want to keep track of animals in the zoo.
An example of a sum type could be an Animal data type. Variables of this data type can only contain one of these 4 values:
- Parrot
- Monkey
- Tiger
- Giraffe
This has the benefit of making invalid state impossible: we are guaranteed that the value of an Animal variable will always be one of the 4 above.
Types of sum-types
edit edit sourceOther algebraic data types
edit edit sourceSee: Algebraic data type