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.

Parameters:

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.

Parameters:

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.

Parameters:

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)() [@property]
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)() [@property]
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() [@property]
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.
auto opBinary(immutable(char)[] op, T)(T rhs)
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.
string 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() [@property]
This can be used to retrieve the TypeInfo for the currently stored value.
void* ptr() [@property]
This can be used to retrieve a pointer to the value stored in the variant.