digitalmars.D.learn - How to initialize the Variant variable with the default value using
- RedCAT (11/11) Jul 15 2017 I have only TypeInfo taken dynamically from an arbitrary type,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (21/26) Jul 16 2017 I'm afraid there is no way of setting the value of Variant dynamically
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. How can I do that? I tried this: TypeInfo ti = typeid(int); Variant v = ti.initializer; But after, int o = v.get!int; and int o = v.coerce!int; doesn't work.
Jul 15 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
Jul 16 2017