October 2014
Intermediate to advanced
280 pages
7h 20m
English
The @spec specifies a function’s parameter count, types, and return-value type. It can appear anywhere in a module that defines the function, but by convention it sits immediately before the function definition, following any function documentation.
We’ve already seen the syntax:
@spec function_name( param1_type, …) :: return_type
Let’s see some examples. These come from the built-in Dict module.
| Line 1 | @type key :: any |
| 2 | @type value :: any |
| 3 | @type keys :: [ key ] |
| 4 | @type t :: tuple | list # `t` is the type of the collection |
| 5 | |
| 6 | @spec values(t) :: [value] |
| 7 | @spec size(t) :: non_neg_integer |
| 8 | @spec has_key?(t, key) :: boolean |
| 9 | @spec update(t, key, value, (value -> value)) :: t |
Read now
Unlock full access