Nesting Variants
evilrat
evilrat666 at gmail.com
Mon May 20 01:55:23 PDT 2013
On Sunday, 19 May 2013 at 23:31:11 UTC, Wyatt wrote:
> I'm trying to use Variants and ran into the following sort of
> situation:
>
> //Using DMD 2.062
> import std.stdio;
> import std.variant;
>
> void main(){
> int key = 1;
> Variant[] one;
> Variant[] ender;
> one = new Variant[](1);
> ender = new Variant[](1);
>
> //This bails:
> //std.variant.VariantException at std/variant.d(1224): Variant:
> attempting to use incompatible types int and
> std.variant.VariantN!(32LU).VariantN
> ender[0] = one;
> ender[0][0] = key;
> writeln(ender[0][0]);
>
> //Also bails only rather than int, it's
> std.variant.VariantN!(32LU).VariantN*:
> //ender[0][0] = new Variant(key);
>
> //This works fine:
> //one[0] = key;
> //ender[0] = one;
> //writeln(ender[0][0]);
> }
>
> The long and short of it seems to be that you can't (easily)
> assign to an element of a Variant array within a Variant array
> but you CAN access it as long as you build the whole thing
> upside-down. Can anyone shed some light on why this is? Am I
> just missing some not-completely-obvious step?
>
> Oh, I should probably mention I was originally using
> associative arrays, so I thought maybe I'd hit one of the bugs
> related to that. But as you can see, it's happening even with
> ordinary arrays.
yes, you forgot to take exact value, it doesn't know anything
about array you put it in, so if you take that array explicitly
and put value on array element it would work
ender[0] = one;
ender[0].get!(Variant[])[0] = key;
writeln(ender[0][0]); // writes 1
also you can check whats inside by doing just this
"writeln(ender);" in case you are not sure what's going on.
More information about the Digitalmars-d-learn
mailing list