View source code
Display the source code in std/sumtype.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aBugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinglocal clone.

Template std.sumtype.get

Accesses a SumType's value.

template get(T);

The value must be of the specified type. Use [has] to check.

Contained Functions

NameDescription
get The actual get function.

Parameters

NameDescription
T the type of the value being accessed.

Example

Basic usage

SumType!(string, double) example1 = "hello";
SumType!(string, double) example2 = 3.14;

writeln(example1.get!string); // "hello"
writeln(example2.get!double); // 3.14

Example

With type qualifiers

alias Example = SumType!(string, double);

Example m = "mutable";
const(Example) c = "const";
immutable(Example) i = "immutable";

writeln(m.get!string); // "mutable"
writeln(c.get!(const(string))); // "const"
writeln(i.get!(immutable(string))); // "immutable"
}

/// As a predicate
version (D_BetterC) {

Example

As a predicate

import std.algorithm.iteration : map;
import std.algorithm.comparison : equal;

alias Example = SumType!(string, double);

auto arr = [Example(0), Example(1), Example(2)];
auto values = arr.map!(get!double);

assert(values.equal([0, 1, 2]));

Authors

Paul Backus

License

Boost License 1.0