RTypes v0.2.0 RTypes View Source
The module defines the derive/1 macro which can be used to derive a
run-time checker for the given type.
The module also defines a function derive/3 which can be used at run
time. However, it must be given arguments as module, type name, and a list of
type args.
Link to this section Summary
Link to this section Functions
derive(code) View Source (macro)
Derive a validating function given the type expression.
The returned function returns either true or false.
derive(mod, type_name, type_args)
View Source
derive(module(), atom(), [RTypes.Extractor.type()]) :: (any() -> boolean())
derive(module(), atom(), [RTypes.Extractor.type()]) :: (any() -> boolean())
The same as derive!/3 but always returns a value.
The returned validating function is typically faster than the 'bang' variant.
derive!(code) View Source (macro)
Derive a validating function given some type expression.
Usage
iex> require RTypes
iex> port_number? = RTypes.derive!(:inet.port_number())
iex> port_number?.(8080)
true
iex> kw_list_of_pos_ints? = RTypes.derive!(Keyword.t(pos_integer()))
iex> kw_list_of_pos_ints?.([a: 1, b: 2])
true
Note that the macro expects the argument as in module.type(arg1, arg2). That
is a module name followed by . and the type name, followed by type
parameters enclosed in parenthesis.
The function raises a run time exception with an explanation of what went wrong.
derive!(mod, type_name, type_args)
View Source
derive!(module(), atom(), [RTypes.Extractor.type()]) ::
(term() -> true | no_return())
derive!(module(), atom(), [RTypes.Extractor.type()]) :: (term() -> true | no_return())
Derive a validating function given a module name, type name and type parameters.
Type arguments must be concrete types, either built-in or basic types like list()
Example
iex> keyword_list? = RTypes.derive!(Keyword, :t, [{:type, 0, :pos_integer, []}])
iex> keyword_list?.(key1: 4, key2: 5)
true
The function raises a run time exception with an explanation of what went wrong.