tyyppi v0.1.0 Tyyppi.T View Source
Raw type wrapper. All the macros exported by that module are available in Tyyppi
.
Require and use Tyyppi
instead.
Link to this section Summary
Functions
Applies the function from the current module, validating input arguments and output.
Experimental: applies the external function given as an argument
in the form &Module.fun/arity
or anonymous function with arguments.
Validates the arguments given and the result produced by the call.
Returns true
if the type definition was loaded, false
otherwise.
Returns true
if the term
passed as the second parameter is of type type
.
Parses the type as by spec and returns its Tyyppi.T
representation.
Link to this section Types
Specs
t() :: %Tyyppi.T{ definition: raw() | nil, module: module(), name: atom(), params: [atom()], quoted: ast(), source: binary(), type: visibility() }
The type information in a human-readable format.
For remote types, it’s gathered from Code.Typespec
, for built-in like atom()
ot’s simply constructed on the fly.
Link to this section Functions
Applies the function from the current module, validating input arguments and output.
See apply/3
for details.
Experimental: applies the external function given as an argument
in the form &Module.fun/arity
or anonymous function with arguments.
Validates the arguments given and the result produced by the call.
Only named types are supported at the moment.
If the number of arguments does not fit the arity of the type, returns
{:error, {:arity, n}}
where n
is the number of arguments passed.
If arguments did not pass the validation, returns {:error, {:args, [arg1, arg2, ...]}}
where argN
are the arguments passed.
If both arity and types of arguments are ok, evaluates the function and checks the
result against the type. Returns {:ok, result}
or {:error, {:result, result}}
if the validation did not pass.
Example:
require Tyyppi.T
Tyyppi.T.apply(MyModule.callback(), MyModule.on_info/1, foo: 2)
#⇒ {:ok,[foo_squared: 4]}
Tyyppi.T.apply(MyModule.callback(), MyModule.on_info/1, foo: :ok)
#⇒ {:error, {:args, :ok}}
Tyyppi.T.apply(MyModule.callback(), MyModule.on_info/1, [])
#⇒ {:error, {:arity, 0}}
Specs
Returns true
if the type definition was loaded, false
otherwise.
Returns true
if the term
passed as the second parameter is of type type
.
Examples:
iex> require Tyyppi.T
...> Tyyppi.T.of?(atom(), :ok)
true
...> Tyyppi.T.of?(atom(), 42)
false
...> Tyyppi.T.of?(GenServer.on_start(), {:error, {:already_started, self()}})
true
...> Tyyppi.T.of?(GenServer.on_start(), :foo)
false
Parses the type as by spec and returns its Tyyppi.T
representation.
Example:
iex> require Tyyppi.T
...> Tyyppi.T.parse(GenServer.on_start()) |> Map.put(:source, nil)
%Tyyppi.T{
definition: {:type, 700, :union,
[
{:type, 0, :tuple, [{:atom, 0, :ok}, {:type, 700, :pid, []}]},
{:atom, 0, :ignore},
{:type, 0, :tuple,
[
{:atom, 0, :error},
{:type, 700, :union,
[
{:type, 0, :tuple,
[{:atom, 0, :already_started}, {:type, 700, :pid, []}]},
{:type, 700, :term, []}
]}
]}
]},
module: GenServer,
name: :on_start,
params: [],
source: nil,
quoted: {{:., [], [GenServer, :on_start]}, [], []},
type: :type
}