Struct Option

Option is a type that contains an optional value.

struct Option(T) ;

Constructors

NameDescription
this (value) Constructs a new option with value.

Properties

NameTypeDescription
get[get] inout(T)Returns the encapsulated value.
get_[get] inout(T)Returns the encapsulated value.
isNothing[get] boolTells if the option is just a value or nothing.
or[set] UReturns the encapsulated value if available or a default value otherwise.

Methods

NameDescription
opAssign (that) Assigns a new value.
opCast () Casts this Option to bool.
opEquals (that) Compares this Option with that.
reset () Resets this Option and destroys the contained value.
toHash () If T has a toHash() method, Option defines toHash() which returns T.toHash() if it is set or 0 otherwise.

Parameters

NameDescription
T Type of the encapsulated value.

See Also

option.

Example

Option!int option;
assert(option.isNothing);
assert(option.or(8) == 8);

option = 5;
assert(!option.isNothing);
assert(option.get == 5);
assert(option.or(8) == 5);

option.reset();
assert(option.isNothing);