Storing arrays as Variant types.

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Dec 23 15:54:00 PST 2014


On Tue, 23 Dec 2014 22:57:07 +0000
"Winter M. via Digitalmars-d-learn" <digitalmars-d-learn at 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".
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141224/40e1e6a9/attachment.sig>


More information about the Digitalmars-d-learn mailing list