diff --git a/lib/algae/maybe.ex b/lib/algae/maybe.ex index 89b83a9..32c8b44 100644 --- a/lib/algae/maybe.ex +++ b/lib/algae/maybe.ex @@ -54,9 +54,40 @@ defmodule Algae.Maybe do iex> new(9) %Algae.Maybe.Just{just: 9} + iex> new(nil) + %Algae.Maybe.Just{just: nil} + + iex> new(nil, nothing: nil) + %Algae.Maybe.Nothing{} + + iex> new(9, nothing: 9) + %Algae.Maybe.Nothing{} + + iex> new(9, nothing: 1) + %Algae.Maybe.Just{just: 9} + + """ + @spec new(any(), [nothing: any()]) :: Just.t() | Nothing.t() + def new(nothing_value, [nothing: nothing_value]), do: Nothing.new() + def new(value, _), do: Just.new(value) + + @spec new(any()) :: Just.t() + def new(value), do: Just.new(value) + + @doc """ + Alias for `new(value, nothing: nil)`. + + ## Examples + + iex> from_nillable(9) + %Algae.Maybe.Just{just: 9} + + iex> from_nillable(nil) + %Algae.Maybe.Nothing{} + """ - @spec new(any) :: Just.t() - defdelegate new(value), to: Just, as: :new + @spec from_nillable(any()) :: Just.t() + def from_nillable(value), do: new(value, nothing: nil) @doc """ Extract a value from a `Maybe`, falling back to a set value in the `Nothing` case.