tango.core.Variant

The variant module contains a variant, or polymorphic type.

License:

BSD style: see license.txt

Authors:

Daniel Keep, Sean Kelly
class VariantTypeMismatchException(T) : Exception #
This exception is thrown whenever you attempt to get the value of a Variant without using a compatible type.
class VariantVoidVarargException : Exception #
This exception is thrown when you attempt to use an empty Variant with varargs.
struct Variant #
The Variant type is used to dynamically store values of different types at runtime.
You can create a Variant using either the pseudo-constructor or direct assignment.

1
2
Variant v = Variant(42);
v = "abc";
Variant opCall(T)(T value) [static] #
This pseudo-constructor is used to place a value into a new Variant.

Params:

valueThe value you wish to put in the Variant.

Returns:

The new Variant.

Example:

1
auto v = Variant(42);
Variant opCall()(TypeInfo type, void* ptr) [static] #
This pseudo-constructor creates a new Variant using a specified TypeInfo and raw pointer to the value.

Params:

typeType of the value.
ptrPointer to the value.

Returns:

The new Variant.

Example:

1
2
int life = 42;
auto v = Variant(typeid(typeof(life)), &life);
Variant opAssign(T)(T value) #
This operator allows you to assign arbitrary values directly into an existing Variant.

Params:

valueThe value you wish to put in the Variant.

Returns:

The new value of the assigned-to variant.

Example:

1
2
Variant v;
v = 42;
bool isA(T)() #
This member can be used to determine if the value stored in the Variant is of the specified type. Note that this comparison is exact: it does not take implicit casting rules into account.

Returns:

true if the Variant contains a value of type T, false otherwise.

Example:

1
2
3
auto v = Variant(cast(int) 42);
assert(   v.isA!(int) );
assert( ! v.isA!(short) ); // note no implicit conversion
bool isImplicitly(T)() #
This member can be used to determine if the value stored in the Variant is of the specified type. This comparison attempts to take implicit conversion rules into account.

Returns:

true if the Variant contains a value of type T, or if the Variant contains a value that can be implicitly cast to type T; false otherwise.

Example:

1
2
3
auto v = Variant(cast(int) 42);
assert( v.isA!(int) );
assert( v.isA!(short) ); // note implicit conversion
bool isEmpty() #
This determines whether the Variant has an assigned value or not. It is simply short-hand for calling the isA member with a type of void.

Returns:

true if the Variant does not contain a value, false otherwise.
void clear() #
This member will clear the Variant, returning it to an empty state.
typeof(T+T) opAdd(T)(T rhs) #
typeof(T+T) opAdd_r(T)(T lhs) #
typeof(T-T) opSub(T)(T rhs) #
typeof(T-T) opSub_r(T)(T lhs) #
typeof(T*T) opMul(T)(T rhs) #
typeof(T*T) opMul_r(T)(T lhs) #
typeof(T/T) opDiv(T)(T rhs) #
typeof(T/T) opDiv_r(T)(T lhs) #
typeof(T%T) opMod(T)(T rhs) #
typeof(T%T) opMod_r(T)(T lhs) #
typeof(T&T) opAnd(T)(T rhs) #
typeof(T&T) opAnd_r(T)(T lhs) #
typeof(T|T) opOr(T)(T rhs) #
typeof(T|T) opOr_r(T)(T lhs) #
typeof(T^T) opXor(T)(T rhs) #
typeof(T^T) opXor_r(T)(T lhs) #
typeof(T<<T) opShl(T)(T rhs) #
typeof(T<<T) opShl_r(T)(T lhs) #
typeof(T>>T) opShr(T)(T rhs) #
typeof(T>>T) opShr_r(T)(T lhs) #
typeof(T>>>T) opUShr(T)(T rhs) #
typeof(T>>>T) opUShr_r(T)(T lhs) #
typeof(T~T) opCat(T)(T rhs) #
typeof(T~T) opCat_r(T)(T lhs) #
Variant opAddAssign(T)(T value) #
Variant opSubAssign(T)(T value) #
Variant opMulAssign(T)(T value) #
Variant opDivAssign(T)(T value) #
Variant opModAssign(T)(T value) #
Variant opAndAssign(T)(T value) #
Variant opOrAssign(T)(T value) #
Variant opXorAssign(T)(T value) #
Variant opShlAssign(T)(T value) #
Variant opShrAssign(T)(T value) #
Variant opUShrAssign(T)(T value) #
Variant opCatAssign(T)(T value) #
The following operator overloads are defined for the sake of convenience. It is important to understand that they do not allow you to use a Variant as both the left-hand and right-hand sides of an expression. One side of the operator must be a concrete type in order for the Variant to know what code to generate.
int opEquals(T)(T rhs) #
int opCmp(T)(T rhs) #
hash_t toHash() #
The following operators can be used with Variants on both sides. Note that these operators do not follow the standard rules of implicit conversions.
char[] toString() #
Returns a string representation of the type being stored in this Variant.

Returns:

The string representation of the type contained within the Variant.
TypeInfo type() #
This can be used to retrieve the TypeInfo for the currently stored value.
void* ptr() #
This can be used to retrieve a pointer to the value stored in the variant.