| Thread overview | |||||
|---|---|---|---|---|---|
|
December 23, 2014 Storing arrays as Variant types. | ||||
|---|---|---|---|---|
| ||||
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. | ||||
December 23, 2014 Re: Storing arrays as Variant types. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Winter M. | 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
| |||
December 23, 2014 Re: Storing arrays as Variant types. | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Winter M. Attachments: | 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 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. 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". | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply