tagged v0.1.0 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
      deftagged error
    end
    
    iex> use Tagged.Status
    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
      deftagged error, as: failure
    end
    
    iex> use Tagged.Outcome
    iex> failure(:is_human)
    {:error, :is_human}
    iex> with success(it) <- {:ok, "Computer"}, do: "OK, #{it}!"
    "OK, Computer!"