Tyyppi.apply
apply
, go back to Tyyppi module for more information.
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.
Examples:
iex> require Tyyppi
...> Tyyppi.apply((atom() -> binary()),
...> fn a -> to_string(a) end, [:foo])
{:ok, "foo"}
...> result = Tyyppi.apply((atom() -> binary()),
...> fn -> "foo" end, [:foo])
...> match?({:error, {:fun, _}}, result)
true
...> Tyyppi.apply((atom() -> binary()),
...> fn _ -> 42 end, ["foo"])
{:error, {:args, ["foo"]}}
...> Tyyppi.apply((atom() -> binary()),
...> fn _ -> 42 end, [:foo])
{:error, {:result, 42}}
Experimental: applies the local 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
Tyyppi.apply(MyModule.callback(), &MyModule.on_info/1, 2)
#⇒ {:ok, [foo_squared: 4]}
Tyyppi.apply(MyModule.callback(), &MyModule.on_info/1, :ok)
#⇒ {:error, {:args, :ok}}
Tyyppi.apply(MyModule.callback(), &MyModule.on_info/1, [])
#⇒ {:error, {:arity, 0}}