tyyppi v0.2.0 Tyyppi View Source

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 local function given as an argument. Validates the arguments given and the result produced by the call.

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 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(). See Tyyppi.T.of?/2 for details.

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().

Parses the type as by spec and returns its Tyyppi.T representation. See Tyyppi.T.parse/1 for details.

Link to this section Functions

Link to this macro

apply(fun, args)

View Source (macro)

Experimental: applies the local function given as an argument. Validates the arguments given and the result produced by the call.

See apply/3 for details.

Link to this macro

apply(type, 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> use 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

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(). See Tyyppi.T.of?/2 for details.

Specs

of_type?(Tyyppi.T.t(), any()) :: boolean()

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().

Examples:

iex> use 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

Parses the type as by spec and returns its Tyyppi.T representation. See Tyyppi.T.parse/1 for details.