optional

Home of the Optional type, faaturing the NotNull type

Modules

bolts
module optional.bolts
Undocumented in source.
dispatch
module optional.dispatch

Provides safe dispatching utilities

notnull
module optional.notnull

Provides a wrapper type to ensure objects are never null

optional
module optional.optional

Optional type

traits
module optional.traits

Optional compile time traits

Public Imports

optional.optional
public import optional.optional;
Undocumented in source.
optional.traits
public import optional.traits;
Undocumented in source.
optional.notnull
public import optional.notnull;
Undocumented in source.
optional.dispatch
public import optional.dispatch;
Undocumented in source.

Examples

// Create empty optional
auto a = no!int;

// Operating on an empty optional is safe and results in none
assert(a == none);
assert(++a == none);

// Assigning a value and then operating yields results
a = 9;
assert(a == some(9));
assert(++a == some(10));

// It is a range
import std.algorithm: map;
auto b = some(10);
auto c = no!int;
assert(b.map!(a => a * 2).equal([20]));
assert(c.map!(a => a * 2).empty);

// Safely get the inner value
assert(b.orElse(3) == 10);
assert(c.orElse(3) == 3);

// Unwrap to get to the raw data (returns a non-null pointer or reference if there's data)
class C {
    int i = 3;
}

auto n = no!C;
if (auto u = n.unwrap) {} else n = some!C(null);
assert(n == none);
if (auto u = n.unwrap) {} else n = new C();
assert(n.unwrap !is null);
assert(n.unwrap.i == 3);

Meta