Maybe

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

Constructors

this
this(T value)

Constructing Maybe from value. If pointer is null methods: isNothing returns true and get throws Error.

Members

Aliases

StoredType
alias StoredType = T

Alias to stored type

Functions

get
T get()

Unwrap value from Maybe. If stored value is null, Error is thrown.

get
const(T) get()

Unwrap value from Maybe. If stored value is null, 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 class A {}
2 
3 auto a = new A();
4 auto ma = Maybe!A(a);
5 auto mb = Maybe!A(null);
6 
7 assert(!ma.isNothing);
8 assert(mb.isNothing);
9 
10 assert(ma.get == a);
11 assertThrown!Error(mb.get);
12 
13 bool ncase = false, jcase = false;
14 ma.map(() {ncase = true;}, (v) {jcase = true;});
15 assert(jcase && !ncase);
16 
17 ncase = jcase = false;
18 mb.map(() {ncase = true;}, (v) {jcase = true;});
19 assert(!jcase && ncase);

Meta