View source code
Display the source code in std/variant.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla 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 using
local clone.
Struct std.variant.VariantN
Back-end type seldom used directly by user
code. Two commonly-used types using VariantN
are:
struct VariantN(ulong maxDataSize, AllowedTypesParam...)
;
Algebraic
: A closed discriminated union with a limited type universe (e.g.,Algebraic!(int, double, string)
only accepts these three types and rejects anything else).Variant
: An open discriminated union allowing an unbounded set of types. If any of the types in theVariant
are larger than the largest built-in type, they will automatically be boxed. This means that even large types will only be the size of a pointer within theVariant
, but this also implies some overhead.Variant
can accommodate all primitive types and all user-defined types.
Both Algebraic
and Variant
share VariantN
's interface. (See their respective documentations below.)
VariantN
is a discriminated union type parameterized
with the largest size of the types stored (maxDataSize
)
and with the list of allowed types (AllowedTypes
). If
the list is empty, then any 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).
Constructors
Name | Description |
---|---|
this
|
Constructs a VariantN value given an argument of a
generic type. Statically rejects disallowed types.
|
this
|
Allows assignment from a subset algebraic type |
Properties
Name | Type | Description |
---|---|---|
coerce [get]
|
T | Returns the value stored in the VariantN object,
explicitly converted (coerced) to the requested type T . If T is a string type, the value is formatted as
a string. If the VariantN object is a string, a
parse of the string to type T is attempted. If a
conversion is not possible, throws a VariantException .
|
convertsTo [get]
|
bool | Returns true if and only if the VariantN
object holds an object implicitly convertible to type T .
Implicit convertibility is defined as per
ImplicitConversionTargets.
|
get [get]
|
inout(T) | Returns the value stored in the VariantN 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 ).
|
hasValue [get]
|
bool | Returns true if and only if the VariantN object
holds a valid value (has been initialized with, or assigned
from, a valid value).
|
length [get]
|
size_t | If the VariantN contains an (associative) array,
returns the length of that array. Otherwise, throws an
exception.
|
peek [get]
|
inout(T)* | If the VariantN 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, peek will not compile.
|
type [get]
|
TypeInfo | Returns the typeid of the currently held value.
|
Methods
Name | Description |
---|---|
opApply
|
If the VariantN contains an array, applies dg to each
element of the array in turn. Otherwise, throws an exception.
|
opAssign
|
Assigns a VariantN from a generic
argument. Statically rejects disallowed types.
|
opBinary
|
Arithmetic between VariantN objects and numeric
values. All arithmetic operations return a VariantN
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opBinaryRight
|
Arithmetic between VariantN objects and numeric
values. All arithmetic operations return a VariantN
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
opCat
|
Arithmetic between VariantN objects and numeric
values. All arithmetic operations return a VariantN
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.
|
opEquals
|
Comparison for equality used by the "==" and "!=" operators. |
opIndex
|
Array and associative array operations. If a VariantN contains an (associative) array, it can be indexed
into. Otherwise, an exception is thrown.
|
opIndexAssign
|
Array and associative array operations. If a VariantN contains an (associative) array, it can be indexed
into. Otherwise, an exception is thrown.
|
opIndexOpAssign
|
Array and associative array operations. If a VariantN contains an (associative) array, it can be indexed
into. Otherwise, an exception is thrown.
|
opOpAssign
|
Arithmetic between VariantN objects and numeric
values. All arithmetic operations return a VariantN
object typed depending on the types of both values
involved. The conversion rules mimic D's built-in rules for
arithmetic conversions.
|
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. |
Example
alias Var = VariantN!(maxSize!(int, double, string));
Var a; // Must assign before use, otherwise exception ensues
// Initialize with an integer; make the type int
Var b = 42;
writeln(b .type); // typeid (int)
// Peek at the value
assert(b .peek!(int) !is null && *b .peek!(int) == 42);
// Automatically convert per language rules
auto x = b .get!(real);
// Assign any other type, including other variants
a = b;
a = 3.14;
writeln(a .type); // typeid (double)
// Implicit conversions work just as with built-in types
assert(a < b);
// Check for convertibility
assert(!a .convertsTo!(int)); // double not convertible to int
// Strings and all other arrays are supported
a = "now I'm a string";
writeln(a); // "now I'm a string"
Example
can also assign arrays
alias Var = VariantN!(maxSize!(int[]));
Var a = new int[42];
writeln(a .length); // 42
a[5] = 7;
writeln(a[5]); // 7
Example
Can also assign class values
alias Var = VariantN!(maxSize!(int*)); // classes are pointers
Var a;
class Foo {}
auto foo = new Foo;
a = foo;
assert(*a .peek!(Foo) == foo); // and full type information is preserved
Authors
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.