Constructs a VariantN value given an argument of a generic type. Statically rejects disallowed types.
Allows assignment from a subset algebraic type
A destructor is present on this object, but not explicitly documented in the source.
A postblit is present on this object, but not explicitly documented in the source.
The list of allowed types. If empty, any type is allowed.
If the VariantN contains an array, applies dg to each element of the array in turn. Otherwise, throws an exception.
Assigns a VariantN from a generic * argument. Statically rejects disallowed types.
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.
Ordering comparison used by the "<", "<=", ">", and ">=" operators. In case comparison is not sensible between the held value and rhs, an exception is thrown.
Comparison for equality used by the "==" and "!=" operators.
Array and associative array operations. If a VariantN contains an (associative) array, it can be indexed into. Otherwise, an exception is thrown.
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.
Computes the hash of the held value.
Formats the stored value as a string.
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.
Returns true if and only if the VariantN object holds an object implicitly convertible to type T. Implicit convertibility is defined as per AllImplicitConversionTargets.
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).
Returns true if and only if the VariantN object holds a valid value (has been initialized with, or assigned from, a valid value).
If the VariantN contains an (associative) array, returns the length of that array. Otherwise, throws an exception.
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.
Returns the typeid of the currently held value.
Tells whether a type T is statically allowed for storage inside a VariantN object by looking T up in AllowedTypes.
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; assert(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; assert(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"; assert(a == "now I'm a string");
can also assign arrays
alias Var = VariantN!(maxSize!(int[])); Var a = new int[42]; assert(a.length == 42); a[5] = 7; assert(a[5] == 7);
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
Back-end type seldom used directly by user code. Two commonly-used types using VariantN are:
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).