View Source TypeCheck.Spec (TypeCheck v0.13.5)

Link to this section Summary

Types

(This type is managed by TypeCheck, which allows checking values against the type at runtime.)

t()

(This type is managed by TypeCheck, which allows checking values against the type at runtime.)

Functions

True if a spec was added to {module, function, arity}.

Looks up the spec for a particular {module, function, arity}.

Looks up the spec for a particular {module, function, arity}.

Link to this section Types

@type problem_tuple() ::
  {t(), :param_error,
   %{
     index: non_neg_integer(),
     problem: TypeCheck.TypeError.Formatter.problem_tuple()
   }, [any()]}
  | {t(), :return_error,
     %{
       arguments: [term()],
       problem: TypeCheck.TypeError.Formatter.problem_tuple()
     }, [any()]}

(This type is managed by TypeCheck, which allows checking values against the type at runtime.)

Full definition:

problem_tuple :: {t(), :param_error,
 %{index: non_neg_integer(), problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple())},
 list(any())}
| {t(), :return_error,
   %{arguments: list(term()), problem: lazy(TypeCheck.TypeError.Formatter.problem_tuple())},
   list(any())}
@type t() :: %TypeCheck.Spec{
  location:
    [] | [file: TypeCheck.DefaultOverrides.String.t(), line: non_neg_integer()],
  name: TypeCheck.DefaultOverrides.String.t(),
  param_types: [TypeCheck.Type.t()],
  return_type: TypeCheck.Type.t()
}

(This type is managed by TypeCheck, which allows checking values against the type at runtime.)

Full definition:

t() :: %TypeCheck.Spec{
  name: TypeCheck.DefaultOverrides.String.t(),
  param_types: list(TypeCheck.Type.t()),
  return_type: TypeCheck.Type.t(),
  location: [] | list({:file, TypeCheck.DefaultOverrides.String.t()} | {:line, non_neg_integer()})
}

Link to this section Functions

Link to this function

defined?(module, function, arity)

View Source

True if a spec was added to {module, function, arity}.

c.f. lookup/3.

iex(1)> defmodule Example3 do
iex(2)>   use TypeCheck
iex(3)>   @spec! greeter(name :: binary()) :: binary()
iex(4)>   def greeter(name), do: "Hello, #{name}!"
iex(5)> end
iex(6)>
iex(7)> TypeCheck.Spec.defined?(Example3, :greeter, 1)
true
iex(8)> TypeCheck.Spec.defined?(Example3, :nonexistent, 0)
false
Link to this function

lookup(module, function, arity)

View Source

Looks up the spec for a particular {module, function, arity}.

On success, returns {:ok, spec}. On failure, returns {:error, :not_found}.

This is quite an advanced low-level function, which you usually won't need to interact with directly.

c.f. lookup!/3.

iex(1)> defmodule Example do
...(2)>   use TypeCheck
...(3)>   @spec! greeter(name :: binary()) :: binary()
...(4)>   def greeter(name), do: "Hello, #{name}!"
...(5)> end
...(6)>
...(7)> {:ok, spec} = TypeCheck.Spec.lookup(Example, :greeter, 1)
...(8)> spec
#TypeCheck.Spec<  greeter(name :: binary()) :: binary() >

iex> TypeCheck.Spec.lookup(Example, :nonexistent, 0)
{:error, :not_found}
Link to this function

lookup!(module, function, arity)

View Source

Looks up the spec for a particular {module, function, arity}.

On success, returns spec. Raises when the spec cannot be found.

c.f. lookup/3.

iex(1)> defmodule Example2 do
...(2)>   use TypeCheck
...(3)>   @spec! greeter(name :: binary()) :: binary()
...(4)>   def greeter(name), do: "Hello, #{name}!"
...(5)> end
...(6)>
...(7)> TypeCheck.Spec.lookup!(Example2, :greeter, 1)
#TypeCheck.Spec<  greeter(name :: binary()) :: binary() >

iex> TypeCheck.Spec.lookup!(Example2, :nonexistent, 0)
** (ArgumentError) No spec found for `Example2.nonexistent/0`