O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required