tagged v0.4.1 Tagged.Status View Source

Resoning in terms of the status of a result.

iex> require Tagged.Status
iex> import Tagged.Status, only: [ok: 1]
iex> ok(:computer)
{:ok, :computer}
iex> with ok(it) <- Keyword.fetch([a: "bacon"], :a), do: "Chunky #{it}!"
"Chunky bacon!"

Link to this section Summary

Functions

error(arg1) deprecated

Constructor error/1 for error tagged value tuples. Can also be used to destructure tuples.

Guard macro for testing if term is a error tagged tuple, with constructor error/1.

Guard macro for testing if term is a ok tagged tuple, with constructor ok/1.

ok(arg1) deprecated

Constructor ok/1 for ok tagged value tuples. Can also be used to destructure tuples.

Calls f/1 with the wrapped value, when term matches a error tagged tuple. When term does not match, is is returned as-is.

Calls f/1 with the wrapped value, when term matches a ok tagged tuple. When term does not match, is is returned as-is.

Link to this section Types

Specs

error() :: {:error, reason :: term()}

Specs

ok() :: {:ok, value :: term()}

Link to this section Functions

This macro is deprecated. Define your own module, with the proper type constraints..

Constructor error/1 for error tagged value tuples. Can also be used to destructure tuples.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> with error(_) <- {:error, 1}, do: true
true
iex> with error(_) <- {:not_error, 1}, do: true
{:not_error, 1}
Link to this macro

is_error(term)

View Source (macro)

Guard macro for testing if term is a error tagged tuple, with constructor error/1.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> f = fn x when is_error(x) -> x; _ -> nil end
iex> error(1) |> f.()
{:error, 1}
iex> {:not_error, 1} |> f.()
nil

Guard macro for testing if term is a ok tagged tuple, with constructor ok/1.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> f = fn x when is_ok(x) -> x; _ -> nil end
iex> ok(1) |> f.()
{:ok, 1}
iex> {:not_ok, 1} |> f.()
nil
This macro is deprecated. Define your own module, with the proper type constraints..

Constructor ok/1 for ok tagged value tuples. Can also be used to destructure tuples.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> with ok(_) <- {:ok, 1}, do: true
true
iex> with ok(_) <- {:not_ok, 1}, do: true
{:not_ok, 1}
Link to this macro

with_error(term, f)

View Source (macro)

Calls f/1 with the wrapped value, when term matches a error tagged tuple. When term does not match, is is returned as-is.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> {:error, 1}
...> |> with_error(fn _ -> :match end)
:match
iex> {:not_error, :miss} |> with_error(& &1)
{:not_error, :miss}
Link to this macro

with_ok(term, f)

View Source (macro)

Calls f/1 with the wrapped value, when term matches a ok tagged tuple. When term does not match, is is returned as-is.

iex> require Elixir.Tagged.Status
iex> import Elixir.Tagged.Status
iex> {:ok, 1}
...> |> with_ok(fn _ -> :match end)
:match
iex> {:not_ok, :miss} |> with_ok(& &1)
{:not_ok, :miss}