View Source Tyyppi (tyyppi v0.12.3)

The main interface to Tyyppi library. Usually, functions and macros presented is this module are enough to work with Tyyppi.

Link to this section Summary

Functions

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.

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.

Returns true if the term passed as the second parameter is of type type. The first parameter is expected to be of type Tyyppi.T.t(term()).

Returns true if the term passed as the second parameter is of type type. The first parameter is expected to be a type as in specs, e. g. atom() or GenServer.on_start().

Parses the type as by spec and returns its Tyyppi.T representation.

Sigil to simplify specification of Tyyppi.T.t(term()) type, it’s literally the wrapper for Tyyppi.parse/1.

Link to this section Functions

Link to this macro

apply(fun, args)

View Source (macro)

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}}
Link to this macro

apply(type, fun, args)

View Source (macro)

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}}
@spec of_type?(Tyyppi.T.t(wrapped), any()) :: boolean() when wrapped: term()

Returns true if the term passed as the second parameter is of type type. The first parameter is expected to be of type Tyyppi.T.t(term()).

Examples:

iex> require Tyyppi
...> type = Tyyppi.parse(atom())
%Tyyppi.T{
  definition: {:type, 0, :atom, []},
  module: nil,
  name: nil,
  params: [],
  quoted: {:atom, [], []},
  source: nil,
  type: :built_in
}
...> Tyyppi.of_type?(type, :ok)
true
...> Tyyppi.of_type?(type, 42)
false
...> type = Tyyppi.parse(GenServer.on_start())
...> Tyyppi.of_type?(type, {:error, {:already_started, self()}})
true
...> Tyyppi.of_type?(type, :foo)
false
Link to this macro

of?(type, term)

View Source (macro)

Returns true if the term passed as the second parameter is of type type. The first parameter is expected to be a type as in specs, e. g. atom() or GenServer.on_start().

Examples:

iex> require Tyyppi
...> Tyyppi.of?(atom(), :ok)
true
...> Tyyppi.of?(atom(), 42)
false
...> Tyyppi.of?(GenServer.on_start(), {:error, {:already_started, self()}})
true
...> Tyyppi.of?(GenServer.on_start(), :foo)
false

Parses the type as by spec and returns its Tyyppi.T representation.

Example:

iex> require Tyyppi
...> parsed = Tyyppi.parse(GenServer.on_start())
...> with %Tyyppi.T{definition: {:type, _, :union, [type | _]}} <- parsed, do: type
{:type, 0, :tuple, [{:atom, 0, :ok}, {:type, 704, :pid, []}]}
...> parsed.module
GenServer
...> parsed.name
:on_start
...> parsed.params
[]
...> parsed.quoted
{{:., [], [GenServer, :on_start]}, [], []}
...> parsed.type
:type
Link to this macro

sigil_t(arg, list)

View Source (macro)

Sigil to simplify specification of Tyyppi.T.t(term()) type, it’s literally the wrapper for Tyyppi.parse/1.

examples

Examples

iex> import Tyyppi
iex> ~t[integer()]
%Tyyppi.T{
  definition: {:type, 0, :integer, []},
  module: nil,
  name: nil,
  params: [],
  quoted: {:integer, [], []},
  source: nil,
  type: :built_in
}
...> ~t[atom]
%Tyyppi.T{
  definition: {:type, 0, :atom, []},
  module: nil,
  name: nil,
  params: [],
  quoted: {:atom, [], []},
  source: nil,
  type: :built_in
}