How to initialize the Variant variable with the default value using TypeInfo?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 16 23:12:35 PDT 2017


On 07/15/2017 09:27 AM, RedCAT wrote:
 > I have only TypeInfo taken dynamically from an arbitrary type, and I
 > need to initialize the Variant variable to the default value for this
 > type.

I'm afraid there is no way of setting the value of Variant dynamically 
because it wants to do type-checking at compile time. You should be able 
to use a switch-case statement yourself because presumably you have the 
set of valid types:

     // (Not compiled.)
     switch (ti) {
     case typeid(int):
         o = v.get!int;

 > TypeInfo ti = typeid(int);
 > Variant v = ti.initializer;

TypeInfo.initializer returns an array of bytes (no type clue there):

     /**
      * Return default initializer.  If the type should be initialized 
to all
      * zeros, an array with a null ptr and a length equal to the type 
size will
      * be returned. For static arrays, this returns the default 
initializer for
      * a single element of the array, use `tsize` to get the correct size.
      */
     abstract const(void)[] initializer() nothrow pure const @safe @nogc;

Ali



More information about the Digitalmars-d-learn mailing list