Optional

Optionals for a given type and with a customizable exception to be thrown when a value is not present and get() is called.

template Optional (
T
onEmptyGet = OptionalException
exceptionArgs...
) if (
isAssignable!(Throwable, onEmptyGet)
) {}

Members

Structs

Optional
struct Optional

The optional itself

Parameters

T

the value type

onEmptyGet

the Throwable to be called when get() is called and no value is present

Examples

Creating an Optional!(T) with no value present and then trying to get the value, which results in an exception

struct MyType
{

}

Optional!(MyType) d;
assert(d.isPresent() == false);
assert(d.isEmpty());

d = Optional!(MyType)();
assert(d.isPresent() == false);
assert(d.isEmpty());

d = Optional!(MyType).empty();
assert(d.isPresent() == false);
assert(d.isEmpty());

try
{
	d.get();
	assert(false);
}
catch(OptionalException)
{
	assert(true);
}

Creating an Optional!(T) with a value present and then trying to get the value, which results in said value being returned

Optional!(byte) f = Optional!(byte)(1);
assert(f.isPresent() == true);
assert(f.isEmpty() == false);

try
{
	assert(1 == f.get());
}
catch(OptionalException)
{
	assert(false);
}

Meta