tango.core.Variant

The variant module contains a variant, or polymorphic type.

License:
BSD style:

Authors:
Daniel Keep, Sean Kelly

class VariantTypeMismatchException: object.Exception;
This exception is thrown whenever you attempt to get the value of a Variant without using a compatible type.

class VariantVoidVarargException: object.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.

  Variant v = Variant(42);
  v = "abc";


Variant opCall(T)(T value);
This pseudo-constructor is used to place a value into a new Variant.

Params:
value The value you wish to put in the Variant.

Returns:
The new Variant.

Example:
  auto v = Variant(42);


Variant opCall()(TypeInfo type, void* ptr);
This pseudo-constructor creates a new Variant using a specified TypeInfo and raw pointer to the value.

Params:
type Type of the value.
ptr Pointer to the value.

Returns:
The new Variant.

Example:
  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:
value The value you wish to put in the Variant.

Returns:
The new value of the assigned-to variant.

Example:
  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:
  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:
  auto v = Variant(cast(int) 42);
  assert( v.isA!(int) );
  assert( v.isA!(short) ); // note implicit conversion


@property 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.

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.

@property TypeInfo type();
This can be used to retrieve the TypeInfo for the currently stored value.

@property void* ptr();
This can be used to retrieve a pointer to the value stored in the variant.

static Variant[] fromVararg(TypeInfo[] types, void* args);
static Variant[] fromVararg(...);
Converts a vararg function argument list into an array of Variants.


Page generated by Ddoc. Copyright (C) 2005-2009 The Tango Team. All rights reserved.