Genericity

Genericity #

A type can have one or several types as parameter(s), represented as type variable(s).

Example. “List of integers” and “list of strings” are two similar yet distinct types.

The type “list of $X$” (where $X$ is a type variable) captures this similarity.

Terminology. In object-oriented languages (e.g. in Java), this is sometimes called a generic type or a generic.

Content #

In this chapter, we will see:

  • types variables in Haskell and Java,
  • limiting the range of a type variable with bounds (for instance, limit $X$ to numeric types) in Haskell and Java,
  • simple generic types in Haskell and Java,
  • the option type (a useful generic type) in Haskell and Java,
  • covariant and contravariant types (i.e. how generic types interact with subtyping), in Java only.