Option

A struct which wraps a value and whether said value is "valid".

Constructors

this
this()
Undocumented in source.

Alias This

hasValue

Members

Functions

get
T get()

Gets the value stored within the Optional.

getOr
T getOr(T value)

Gets the value stored within the Optional if valid, otherwise returns the given value.

reset
void reset()

Destroys the contained value.

Properties

hasValue
bool hasValue [@property getter]

Whether this instance contains a valid value.

Static functions

none
Option!T none()

Creates a new empty value.

some
Option!T some(T value)

Wraps value in an optional type.

Examples

Option!int positiveOnly(int value) @nogc nothrow {
    return value >= 0 ? some(value) : none!int();
}

auto rval = positiveOnly(4);
if (rval) {
    writeln("Value is positive!");
} else {
    writeln("Value is negative!");
}

Meta