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 section Functions
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)
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)
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, {}}
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