www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Storing arrays as Variant types.

reply "Winter M." <Winter.M cloudchaser.io> writes:
I've run into a problem while trying to coerce array values from 
a variant; specifically,

char[] a = aVariant.coerce!(char[]); // This works just fine.

byte[] b = bVariant.coerce!(byte[]); // This causes a static 
assertion to fail.

I'm not really sure why a byte[] would be an unsupported type, 
since memory-wise the reference should take up as much space as 
for the char[] (as I understand it).
Perhaps I'm missing something, but I'm lost as to why this is the 
case.

Thanks.
Dec 23 2014
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
Minimal code for convenience to others:

import std.variant;

void main()
{
     Variant aVariant;
     Variant bVariant;

     char[] a = aVariant.coerce!(char[]);
     byte[] b = bVariant.coerce!(byte[]);
}

On 12/23/2014 02:57 PM, Winter M. wrote:

 I've run into a problem while trying to coerce array values from a
 variant; specifically,

 char[] a = aVariant.coerce!(char[]); // This works just fine.

 byte[] b = bVariant.coerce!(byte[]); // This causes a static assertion
 to fail.

 I'm not really sure why a byte[] would be an unsupported type, since
 memory-wise the reference should take up as much space as for the char[]
 (as I understand it).
 Perhaps I'm missing something, but I'm lost as to why this is the case.
The difference is that char[] passes the isSomeString test (as it is a string) but byte[] does not: https://github.com/D-Programming-Language/phobos/blob/master/std/variant.d#L877 Ali
Dec 23 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Tue, 23 Dec 2014 22:57:07 +0000
"Winter M. via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com>
wrote:

 I've run into a problem while trying to coerce array values from=20
 a variant; specifically,
=20
 char[] a =3D aVariant.coerce!(char[]); // This works just fine.
=20
 byte[] b =3D bVariant.coerce!(byte[]); // This causes a static=20
 assertion to fail.
=20
 I'm not really sure why a byte[] would be an unsupported type,=20
 since memory-wise the reference should take up as much space as=20
 for the char[] (as I understand it).
 Perhaps I'm missing something, but I'm lost as to why this is the=20
 case.
heh. this is due to how `.coerce!` written. it doesn't really checks for arrays, what it checks for is: 1. static if (isNumeric!T || isBoolean!T) 2. static if (is(T : Object)) 3. static if (isSomeString!(T)) see the gotcha? ;-) both types you requested are not numeric, not boolean and not objects. but `char[]` satisfies `isSomeString!`, and `byte[]` doesn't. i don't sure that coercing is designed to work this way, it seems that `isSomeString!` is just a hack for coercing to strings. i.e. with `char[]` variant tries to build some textual representation of it's value, and with `byte[]` variant simply don't know what to do. maybe we should allow coercing to `byte[]` and `ubyte[]` with the defined meaning: "get raw binary representation of variant contents".
Dec 23 2014