plymio_codi v0.3.1 Plymio.Codi.Pattern.Typespec View Source

The typespec patterns build typespec module attributes.

Currently only @spec is supported.

See Plymio.Codi for an overview and documentation terms.

Convenience Aliases

A number of convenience aliases are available to simplify the typespec declaration, especially the result. Note the Meaning in the following table is quoted form (so, e.g. :ok_atom means quote(do: {:ok, atom}))

AliasMeaning
:result{:ok, any} | {:error, error}
:ok_result{:ok, any}
:error_result{:error, error}
:bang_resultany | no_return
:ok_atom{:ok, atom}
:ok_binary{:ok, binary}
:ok_list{:ok, list}
:ok_keyword{:ok, keyword}
:ok_opts{:ok, keyword}
:ok_map{:ok, map}
:ok_tuple{:ok, tuple}
:ok_integer{:ok, integer}
:ok_float{:ok, float}
:ok_struct{:ok, struct}
:ok_t{:ok, t}
:atom_result{:ok, atom} | {:error, error}
:binary_result{:ok, binary} | {:error, error}
:list_result{:ok, list} | {:error, error}
:keyword_result{:ok, keyword} | {:error, error}
:opts_result{:ok, keyword} | {:error, error}
:map_result{:ok, map} | {:error, error}
:tuple_result{:ok, tuple} | {:error, error}
:integer_result{:ok, integer} | {:error, error}
:float_result{:ok, float} | {:error, error}
:struct_result{:ok, struct} | {:error, error}
:t_result{:ok, t} | {:error, error}

Pattern: typespec_spec

The typespec_spec pattern builds a @spec form.

Valid keys in the pattern opts are:

KeyAliases
:typespec_spec_name:name :spec_name, :fun_name, :function_name
:typespec_spec_args:args, :spec_args, :fun_args, :function_args
:typespec_spec_arity:arity, :spec_arity, :fun_arity, :function_arity
:typespec_spec_result:result, :spec_result, :fun_result, :function_result

Examples

When an :arity is given, the :spec_args will all be any.

iex> {:ok, {forms, _}} = [
...>   spec: [name: :fun1, arity: 1, result: :integer]#
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@spec(fun1(any) :: integer)"]

The function’s args can be given explicitly. Here a list of atoms are given which will be normalised to the equivalent type var. Note also the :spec_result is an explicit form.

iex> spec_result = quote(do: binary | atom)
...> {:ok, {forms, _}} = [
...>   spec: [spec_name: :fun2, args: [:atom, :integer], spec_result: spec_result]
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@spec(fun2(atom, integer) :: binary | atom)"]

These examples use the convenience aliases.

iex> {:ok, {forms, _}} = [
...>   spec: [spec_name: :fun4, args: [:atom, :integer], spec_result: :atom_result]
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@spec(fun4(atom, integer) :: {:ok, atom} | {:error, error})"]

iex> {:ok, {forms, _}} = [
...>   spec: [spec_name: :fun4, args: [:atom, :integer],
...>   spec_result: [:ok_tuple, :ok_map, :error_result]]
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@spec(fun4(atom, integer) :: {:ok, tuple} | {:ok, map} | {:error, error})"]

iex> {:ok, {forms, _}} = [
...>   spec: [spec_name: :fun4, args: :struct, spec_result: :struct_result]
...> ] |> produce_codi
...> forms |> harnais_helper_show_forms!
["@spec(fun4(struct) :: {:ok, struct} | {:error, error})"]