Maybe

Struct-wrapper to handle result of computations, that can fail.

Constructors

this
this(T value)

Constructing Maybe from value. If Maybe is created with the constructor, it is considered non empty and isNothing returns false.

Members

Aliases

StoredType
alias StoredType = T

Alias to stored type

Functions

get
T get()

Unwrap value from Maybe. If the Maybe is empty, Error is thrown.

get
const(T) get()

Unwrap value from Maybe. If the Maybe is empty, Error is thrown.

isNothing
bool isNothing()

Returns true if stored value is null

map
U map(U delegate() nothingCase, U delegate(T) justCase)

If struct holds null, then nothingCase result is returned. If struct holds not null value, then $(justCase) result is returned. justCase is fed with unwrapped value.

map
U map(U delegate() nothingCase, U delegate(const T) justCase)

If struct holds null, then nothingCase result is returned. If struct holds not null value, then $(justCase) result is returned. justCase is fed with unwrapped value.

Static functions

nothing
Maybe!T nothing()

Constructing empty Maybe. If Maybe is created with the method, it is considred empty and isNothing returns false.

Examples

Example

1 struct A {}
2 
3 auto ma = Maybe!A(A());
4 auto mb = Maybe!A.nothing;
5 
6 assert(!ma.isNothing);
7 assert(mb.isNothing);
8 
9 assert(ma.get == A());
10 assertThrown!Error(mb.get);
11 
12 bool ncase = false, jcase = false;
13 ma.map(() {ncase = true;}, (v) {jcase = true;});
14 assert(jcase && !ncase);
15 
16 ncase = jcase = false;
17 mb.map(() {ncase = true;}, (v) {jcase = true;});
18 assert(!jcase && ncase);

Meta