Optional

ditto

Members

Functions

get
bool get(T value)

A more "old-school" version of visit. Provided both to conform style of existing code and avoid delegate bugs in dmd1.

isDefined
bool isDefined()
opAssign
void opAssign(T rhs)

Puts this into defined state

reset
void reset()

Puts this into undefined state

visit
void visit(void delegate() cb_undefined, void delegate(ref T) cb_defined)

Interface to retrieve stored value. It is intentionally designed in a way that forces you to handle "undefined" state to avoid issues akin to "null pointer".

Manifest constants

undefined
enum undefined;

Alias to make code working with undefined state more readable

Examples

alias Optional!(bool) Maybe;

Maybe x, y, z;
x = true;
y = false;
z = Maybe.undefined;

x.visit(
    ()               { test(false); },
    (ref bool value) { test(value); }
);

y.visit(
    ()               { test(false); },
    (ref bool value) { test(!value); }
);

z.visit(
    ()               { test(true); },
    (ref bool value) { test(false); }
);

Meta