Learning path

Full curriculum

Full curriculum

Unit content

Parameterized types, sum types and product types

A type can be built systematically from other types. These constructions let programs represent alternatives and combinations explicitly instead of relying on informal conventions.

Product types

A product type contains several values together. A pair

$$A\times B$$

contains one value of type $A$ and one value of type $B$.

Records, tuples and structures are common programming forms of product types.

Sum types

A sum type represents one of several alternatives. An optional value can be modeled as

None
or
Some(value)

while a fallible computation can return

Error(reason)
or
Success(value)

The alternatives are part of the type rather than being encoded through special sentinel values.

Parameterized types

A type constructor can work uniformly for many contained types. Option<T>, for example, means either no value or one value of type T.

Likewise, Result<E,T> can represent either an error of type E or a successful value of type T.

Making cases explicit

Code consuming a sum type must account for the possible alternatives. This turns absence and failure into ordinary data that can be transformed and composed deliberately.

Parameterized sum and product types provide the vocabulary used by functional abstractions such as map, flatMap and monadic composition.