Specifying a Type
Explore how to define and use type specifications in Elixir. Understand basic and composite types, type unions, structs, and anonymous functions to write clearer and more reliable code.
We'll cover the following...
A type is simply a subset of all possible values in a language. For example, the type integer means all the possible integer values but excludes lists, binaries, PIDs, and so on.
The basic types in Elixir are as follows:
any, atom, float, fun, integer, list, map, maybe_improper_list, none, pid, port, reference, struct, and tuple.
The type any (and its alias, _) is the set of all values, and none is the empty set. A literal atom or integer is the set containing just that value.
The value nil can be represented as nil.
Collection types
A list is represented as [type], where type is any of the basic or combined types. This notation doesn’t signify a list of one element. It simply says that elements of the list will be of the given type. If we want to specify a nonempty list, we use [type, ...]. As a convenience, the type list is ...