Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.

dmd.root.optional

Implementation of an 'Optional' type
Authors:
Examples:
import core.exception : AssertError;

Optional!int opt;
assert( opt.isEmpty());
assert(!opt.isPresent());
assert(!opt.hasValue(1));
assert(!opt.hasValue(2));

bool caught;
try
    cast(void) opt.get();
catch (AssertError)
    caught = true;
assert(caught);

opt = Optional!int(1);
assert(!opt.isEmpty());
assert( opt.isPresent());
assert( opt.get() == 1);
assert( opt.hasValue(1));
assert(!opt.hasValue(2));
struct Optional(T);
Optional type that is either empty or contains a value of type T
this(T value);

static Optional!T create(T val);
Creates an Optional with the given value
const bool isPresent();
Returns:
Whether this Optional contains a value
const bool isEmpty();
Returns:
Whether this Optional does not contain a value
inout inout(T) get();
Returns:
The value if present
const bool hasValue(const T exp);
Returns:
Whether this Optional contains the supplied value