std.variant.variant
- multiple declarations
- Struct VariantN
- Alias Variant
Struct VariantN
is a back-end VariantN
type
seldom used directly by user
code. Two commonly-used types using
as
back-end are:
VariantN
Algebraic
: A closed discriminated union with a limitedtype
universe (e.g.,
only accepts these three types and rejects anything else).Algebraic
!(int, double, string)Variant
: An open discriminated union allowing an unbounded set of types. If any of the types in the
are larger than the largest built-inVariant
type
, they will automatically be boxed. This means that even large types will only be the size of a pointer within the
, but this also implies some overhead.Variant
can accommodate all primitive types and all user-defined types.Variant
Both
and Algebraic
share Variant
's interface. (See their respective documentations below.)
VariantN
is a discriminated union VariantN
type
parameterized
with the largest size of the types stored (maxDataSize
)
and with the list of allowed
types (
). If
the list is empty, then any AllowedTypes
type
up of size up to maxDataSize
(rounded up for alignment) can be stored in a
VariantN
object
without being boxed (types larger
than this will be boxed).
Properties
Name | Type | Description |
---|---|---|
hasValue
[get]
|
bool |
Returns true if and only if the object
holds a valid value (has been initialized with, or assigned
from, a valid value).
|
length
[get]
|
size_t |
If the contains an (associative) array,
returns the length of that array. Otherwise, throws an
exception.
|
type
[get]
|
TypeInfo |
Returns the typeid of the currently held value.
|
Methods
Name | Description |
---|---|
toHash
|
Computes the hash of the held value. |
toString
|
Formats the stored value as a string. |
Aliases
Name | Description |
---|---|
AllowedTypes
|
The list of allowed types. If empty, any type is allowed .
|
Templates
Name | Description |
---|---|
allowed
|
Tells whether a type T is statically allowed for
storage inside a object by looking
T up in .
|
coerce
|
Returns the value stored in the object ,
explicitly converted (coerced) to the requested type T . If T is a string type , the value is formatted as
a string. If the object is a string, a
parse of the string to type T is attempted. If a
conversion is not possible, throws a .
|
convertsTo
|
Returns true if and only if the
object holds an object implicitly convertible to type U . Implicit convertibility is defined as per
ImplicitConversionTargets.
|
get
|
Returns the value stored in the object , either by specifying the
needed type or the index in the list of allowed types. The latter overload
only applies to bounded variants (e.g. Algebraic ).
|
opAdd
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opAddAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opAnd
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opAndAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opApply
|
If the contains an array, applies dg to each
element of the array in turn. Otherwise, throws an exception.
|
opAssign
|
Assigns a from a generic
argument. Statically rejects disallowed types.
|
opCat
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opCatAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opCmp
|
Ordering comparison used by the "<", "<=", ">", and ">="
operators. In case comparison is not sensible between the held
value and rhs , an exception is thrown.
|
opDiv
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opDivAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opEquals
|
Comparison for equality used by the "==" and "!=" operators. |
opIndex
|
Array and associative array operations. If a contains an (associative) array, it can be indexed
into. Otherwise, an exception is thrown.
|
opIndexAssign
|
Array and associative array operations. If a contains an (associative) array, it can be indexed
into. Otherwise, an exception is thrown.
|
opMod
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opModAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opMul
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opMulAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opOr
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opOrAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opShl
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opShlAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opShr
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opShrAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opSub
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opSubAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opUShr
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opUShrAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opXor
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opXorAssign
|
Arithmetic between objects and numeric
values. All arithmetic operations return a
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
peek
|
If the object holds a value of the
exact type T , returns a pointer to that
value. Otherwise, returns null . In cases
where T is statically disallowed, will not compile.
|
this
|
Allows assignment from a subset algebraic type
|
this
|
Constructs a value given an argument of a
generic type . Statically rejects disallowed types.
|
Alias Variant
is an alias for Variant
instantiated with the largest of VariantN
creal
,
char[]
, and void delegate()
. This ensures that
is large enough
to hold all of D's predefined types unboxed, including all numeric types,
pointers, delegates, and class references. You may want to use
Variant
directly with a different maximum size either for
storing larger types unboxed, or for saving memory.
VariantN
Declaration
alias Variant = VariantN!(32L)
;