On Sun, May 19, 2013 at 4:31 PM, Wyatt <wyatt....@gmail.com> 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@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.
>



I think I have a solution for you:

see thread:
http://forum.dlang.org/post/mailman.1054.1371029915.13711.digitalmar...@puremagic.com


 
<https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d>
>
> https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d
>        auto
> d=variantTupleNamed("a",1,"b","foo","c",variantTuple(1,2.2,"three"));
> d["a"]=2;
> auto v=d["c"][0].get!int;//can coerce to int
> v+=3;
> d["c"][0]="other1";//can access nested type
> d["a"]="other2";//can change type
> d["a"]=variantTuple(0.0,'e');
> d["a"]=10;
> d["a"]+=2; //read-modify-write works, unlike std.variant : 'Due to
> limitations in current language, read-modify-write operations op= will not
> work properly'
> assert(d.text==`["a":12, "b":foo, "c":[other1, 2.2, three]]`);
> Pending DIP32 (Uniform tuple syntax), this could improve to:
>        auto d=variant( {a=1,b="foo", c={1,2.2,"three"}} );

Reply via email to