Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Algae.Maybe.new(nil) return a Nothing #27

Merged
merged 2 commits into from
Jul 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions lib/algae/maybe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down