Using the Variant (Setting it's memory location)

Jesse Phillips jessekphillips+D at gmail.com
Mon Feb 6 15:08:38 PST 2012


On Sunday, 5 February 2012 at 20:20:58 UTC, Era Scarecrow wrote:
> The data I will be accessing has dozens of different structures 
> of records which I keep the formatting and types saved in an 
> array. I see the problem where although variant can handle what 
> I need, but I don't see a way to specify a specific memory 
> range/access in order to use it.

Variant allows you to store any value, but it does not need a 
buffer or have interaction like an array. It can hold any value 
because it the size of the data structure is the max size of the 
times it can hold. For this reason it can not actually hold any 
structure.

For an array of any values you use a Variant[] foo = new 
Variant[100].

foo[0] = myInt;
foo[1] = myFloat;

However depending on the length of char[static size], you may 
need to give Variant a larger maximum size.

VariantN!(mySize)[]...

You can also use the Algebraic type to specified allowed types:

Algebraic!(int,float,char[3],char[100]...)[]...

This will calculate the max size you need and only allow those 
times to be assigned.

Accessing is different. The Variant type does not convert to any 
type, so you must know the type or ask if it is a type.

foo.get!int()

if(foo[0].type == int) // I think that was valid check. Maybe 
typeid(int)
    foo[0].get!int()


More information about the Digitalmars-d-learn mailing list