union_type v0.1.1 UnionType

Link to this section Summary

Functions

Like Kernel.elem/2, but operates on the values of the union type.

Deserializes the Elixir tuple into a union type.

Serializes the union type value into a plain Elixir tuple.

Provides a mechanism for defining a union type.

Link to this section Types

Link to this type

t()

t() :: %UnionType{__module__: module(), __name__: atom(), __values__: tuple()}

Link to this section Functions

Link to this function

at(union_type, index)

at(t(), non_neg_integer()) :: term()

Like Kernel.elem/2, but operates on the values of the union type.

Examples

iex> UserEnum.customer("John") |> UnionType.at(0)
"John"

iex> UserEnum.contractor("Erin", "TechCo") |> UnionType.at(1)
"TechCo"
Link to this function

from_tuple(arg)

from_tuple({module(), variant_name :: atom(), tuple()}) :: t()

Deserializes the Elixir tuple into a union type.

Examples

iex> UnionType.from_tuple({UserEnum, :customer, {"John"}})
UserEnum.customer("John")

iex> UnionType.from_tuple({UserEnum, :admin, {"Linda"}})
UserEnum.admin("Linda")

iex> UnionType.from_tuple({UserEnum, :contractor, {"Erin", "TechCo"}})
UserEnum.contractor("Erin", "TechCo")

iex> UnionType.from_tuple({UserEnum, :guest, {}})
UserEnum.guest()
Link to this function

to_tuple(union_type)

to_tuple(t()) :: {module(), variant_name :: atom(), tuple()}

Serializes the union type value into a plain Elixir tuple.

Examples

iex> UserEnum.customer("John") |> UnionType.to_tuple()
{UserEnum, :customer, {"John"}}

iex> UserEnum.admin("Linda") |> UnionType.to_tuple()
{UserEnum, :admin, {"Linda"}}

iex> UserEnum.contractor("Erin", "TechCo") |> UnionType.to_tuple()
{UserEnum, :contractor, {"Erin", "TechCo"}}

iex> UserEnum.guest() |> UnionType.to_tuple()
{UserEnum, :guest, {}}
Link to this macro

union_type(list)

(macro)

Provides a mechanism for defining a union type.

Example

iex> defmodule AuthEnum do
...>   use UnionType
...>
...>   union_type do
...>     admin(level)
...>     none()
...>   end
...> end
...>
...> defmodule App do
...>   require AuthEnum
...>
...>   def grant_access?(AuthEnum.admin(level)) do
...>     level == :top
...>   end
...>
...>   def grant_access?(AuthEnum.none()) do
...>     false
...>   end
...>
...>   def check, do: grant_access?(AuthEnum.admin(:top))
...> end
...>
...> App.check()
true