tagged v0.4.1 Tagged.Constructor View Source
Generates macros for constructing, and destructuring tagged value tuples.
By default, the macro name is the same as the tag, but can be overriden with
the as: name
keyword argument.
This module is always executed.
Examples
Define constructors with the same name as the tag
defmodule Tagged.Status use Tagged deftagged ok(term()) deftagged error(term()) end 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!"
Override constructor name
defmodule Tagged.Outcome use Tagged deftagged ok, as: success(term()) deftagged error, as: failure(term()) end iex> require Tagged.Outcome iex> import Tagged.Outcome, only: [failure: 1, success: 1] iex> failure(:is_human) {:error, :is_human} iex> with success(it) <- {:ok, "Computer"}, do: "OK, #{it}!" "OK, Computer!"