Defining New Types

The attribute @type can be used to define new types.

 
@type​ type_name :: type_specification

Elixir uses this to predefine some built-in types and aliases.

 
@type​ term :: any
 
@type​ binary :: <<_::_*8>>
 
@type​ bitstring :: <<_::_*1>>
 
@type​ boolean :: ​false​ | ​true
 
@type​ byte :: 0..255
 
@type​ char :: 0..0x10ffff
 
@type​ list :: [ any ]
 
@type​ list(t) :: [ t ]
 
@type​ number :: integer | float
 
@type​ module :: atom
 
@type​ mfa :: {module, atom, byte}
 
@type​ node :: atom
 
@type​ timeout :: :infinity | non_neg_integer
 
@type​ no_return :: none

As the list entries show, you can parameterize the types in a new definition. Simply use one or more identifiers as parameters on the left side, and use these ...

Get Programming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.