Elixir CICoverage StatusHex packageHexdocsLicense

Tagged

Generates definitions of various things related to tuples with a tagged value, such as the ubiquitous {:ok, value} and {:error, reason}.

Examples

defmodule Status
use Tagged
deftagged ok(value :: term())
deftagged error(reason :: term())
end

Construct and Destructure

iex> require Status
iex> import Status
iex> ok(:computer)
{:ok, :computer}
iex> with error(reason) <- {:ok, :computer}, do: raise reason
{:ok, :computer}
iex> with error(reason) <- {:error, "OH NO!"}, do: raise reason
** (RuntimeError) OH NO!

Type definitions

_iex> require Status
_iex> t Status.error
@type error() :: {:error, reason :: term()}

Selective execution with unwrapped value

iex> require Status
iex> import Status
iex> ok(:computer) |> with_ok(& "OK, #{&1}")
"OK, computer"
iex> error("OH NO!") |> with_ok(& "OK, #{&1}")
{:error, "OH NO!"}

Sum Algebraic Data Types

A module that defines some tagged values, a composit type, and guard of those, forms a Sum Algebraic Data Type, also known as a Tagged Union.

defmodule BinTree do
use Tagged
deftagged tree(left :: t(), right :: t())
deftagged leaf(value :: term())
deftagged nil, as: empty()
@type t() :: tree() | leaf() | empty()
defguard is_t(x) when is_tree(x) or is_leaf(x) or is_empty(x)
end
iex> require BinTree
iex> import BinTree
iex> t = tree(leaf(1),
...> tree(leaf(2),
...> empty()))
{:tree, {:leaf, 1}, {:tree, {:leaf, 2}, nil}}
iex> is_t(t)
true

Installation

The package can be installed by adding tagged to your list of dependencies in mix.exs:

def deps do
[
{:tagged, "~> 0.4.1"}
]
end