-
Notifications
You must be signed in to change notification settings - Fork 35
Type annotations
By including the below header file you get access to some utility syntax elements for type annotations.
-include_lib("gradualizer/include/gradualizer.hrl").
If you don't want a hard dependency on Gradualizer in your project, you can copy the content to your project instead.
The macro ?annotate_type/2
can be used to annotate an expression with a
type. This is useful to add type annotation after fetching a value from an
ETS table, received a message on a known form or read from a dict, proplist,
process dictionary, etc.
The specified type must be compatible with the type detected by Gradualizer. Otherwise a type error is reported.
N = ?annotate_type( Message, non_neg_integer() )
The macro ?assert_type/2
can be used to refine (downcast) a type propagated
by Gradualizer. For example, the programmer may know that the length of a
list is within a certain range, rather than any non_neg_integer()
:
Arity = ?assert_type( length(Args), arity() )
The functions '::'/2
and ':::'/2
(respectively type annotation
and assertion) can also be used directly if the type is quoted:
N = '::'(Message, "non_neg_integer()")
Gradualizer detects occurences of the functions '::'/2
and
':::'/2
and adjusts type checking accordingly. The functions
are inlined by the compiler and have no runtime overhead. The
macros are supplied only for convenience.
Elixir support is in development. There will be annotate_type/2
and
assert_type/2
macros provided.