Struct Variant

Type that can hold one of the types listed as its template parameters.

struct Variant(Specs...)
  
if (isTypeTuple!Specs && (NoDuplicates!Specs.length == Specs.length));

Variant is a type similar to union, but Variant keeps track of the actually used type and throws an assertion error when trying to access an invalid type at runtime.

Constructors

NameDescription
this (value) Constructs this Variant with one of the types supported in it.

Properties

NameTypeDescription
type[get] TypeInfoReturns TypeInfo corresponding to the current type.

Methods

NameDescription
get () Returns the underlying value, assuming it is of the type T.
hasValue () Tells whether this Variant is initialized.
opAssign (that) Reassigns the value.
opEquals (that) Compares this Variant with another one with the same specification for equality.
peek () Tells whether this Variant holds currently a value of type T.

Aliases

NameDescription
Types Types can be present in this Variant.

Parameters

NameDescription
Specs Types this Variant can hold.

Example

Variant!(int, double) variant = 5;
assert(variant.peek!int);
assert(variant.get!int == 5);

variant = 5.4;
assert(!variant.peek!int);
assert(variant.get!double == 5.4);