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
Name | Description |
this
(value)
|
Constructs this Variant with one of the types supported
in it.
|
Properties
Name | Type | Description |
type [get]
|
TypeInfo | Returns TypeInfo corresponding to the current type.
|
Methods
Name | Description |
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
Name | Description |
Types
|
Types can be present in this Variant.
|
Parameters
Name | Description |
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);