tryGet

Attempt to access a SumType's value.

If the SumType does not contain a value of the specified type, an exception is thrown.

template tryGet(T)
version(D_Exceptions)
ref
T
tryGet
(
Self
)
(
auto ref Self self
)
if ()

Members

Functions

tryGet
T tryGet(Self self)

The actual tryGet function.

Parameters

T

the type of the value being accessed.

Examples

Basic usage

SumType!(string, double) example = "hello";

assert(example.tryGet!string == "hello");

double result = double.nan;
try
	result = example.tryGet!double;
catch (MatchException e)
	result = 0;

// Exception was thrown
assert(result == 0);

With type qualifiers

import std.exception: assertThrown;

const(SumType!(string, double)) example = "const";

// Qualifier mismatch; throws exception
assertThrown!MatchException(example.tryGet!string);
// Qualifier matches; no exception
assert(example.tryGet!(const(string)) == "const");

Meta