Skip to content
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
13 changes: 7 additions & 6 deletions std/typecons.d
Original file line number Diff line number Diff line change
Expand Up @@ -3441,28 +3441,29 @@ if (is (typeof(nullValue) == T))
assert(ntts.to!string() == "2.5");
}

// apply
/**
Unpacks the content of a $(D Nullable), performs an operation and packs it again. Does nothing if isNull.

When called on a $(D Nullable), $(D apply) will unpack the value contained in the $(D Nullable),
pass it to the function you provide and wrap the result in another $(D Nullable) (if necessary).
If the Nullable is null, $(D apply) will return null itself.
When called on a `Nullable`, `apply` will unpack the value contained in the `Nullable`,
Copy link
Contributor

@wilzbach wilzbach Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should continue the automation alternative here. At the moment we only converted std.algorithm automatically (see #5970)

-> #6391

pass it to the function you provide and wrap the result in another `Nullable` (if necessary).
If the `Nullable` is null, `apply` will return null itself.

Params:
t = a $(D Nullable)
t = a `Nullable`
fun = a function operating on the content of the nullable

Returns:
`fun(t.get).nullable` if `!t.isNull`, else `Nullable.init`.

See also:
$(HTTP en.wikipedia.org/wiki/Monad_(functional_programming)#The_Maybe_monad, The `Maybe` monad)
$(HTTPS en.wikipedia.org/wiki/Monad_(functional_programming)#The_Maybe_monad, The `Maybe` monad)
*/
template apply(alias fun)
{
import std.functional : unaryFun;

auto apply(T)(T t)
auto apply(T)(auto ref T t)
if (isInstanceOf!(Nullable, T) && is(typeof(unaryFun!fun(T.init.get))))
{
alias FunType = typeof(unaryFun!fun(T.init.get));
Expand Down