July 15, 2017 How to initialize the Variant variable with the default value using TypeInfo? | ||||
---|---|---|---|---|
| ||||
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. |
July 16, 2017 Re: How to initialize the Variant variable with the default value using TypeInfo? | ||||
---|---|---|---|---|
| ||||
Posted in reply to RedCAT | 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 |
Copyright © 1999-2021 by the D Language Foundation