<div>On Sun, May 19, 2013 at 4:31 PM, Wyatt <span dir="ltr"><<a href="mailto:wyatt.epp@gmail.com" target="_blank">wyatt.epp@gmail.com</a>></span> wrote:</div><div><div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm trying to use Variants and ran into the following sort of situation:<br>
<br>
//Using DMD 2.062<br>
import std.stdio;<br>
import std.variant;<br>
<br>
void main(){<br>
        int key = 1;<br>
        Variant[] one;<br>
        Variant[] ender;<br>
        one = new Variant[](1);<br>
        ender = new Variant[](1);<br>
<br>
        //This bails:<br>
        //std.variant.<u></u>VariantException@std/variant.<u></u>d(1224): Variant: attempting to use incompatible types int and std.variant.VariantN!(32LU).<u></u>VariantN<br>
        ender[0] = one;<br>
        ender[0][0] = key;<br>
        writeln(ender[0][0]);<br>
<br>
        //Also bails only rather than int, it's std.variant.VariantN!(32LU).<u></u>VariantN*:  <br>
        //ender[0][0] = new Variant(key);<br>
<br>
        //This works fine:<br>
        //one[0] = key;<br>
        //ender[0] = one;<br>
        //writeln(ender[0][0]);<br>
}<br>
<br>
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?<br>

<br>
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.<br>
</blockquote></div><br></div></div><div><br></div><div><br></div><div><div>I think I have a solution for you:</div><div><br></div><div>see thread:</div><a href="http://forum.dlang.org/post/mailman.1054.1371029915.13711.digitalmars-d@puremagic.com">http://forum.dlang.org/post/mailman.1054.1371029915.13711.digitalmars-d@puremagic.com</a><div>
<br></div><div><pre class="post-text" style="margin-top:0px;margin-bottom:0px;font-family:Consolas,monospace;font-size:14px;white-space:pre-wrap;word-wrap:break-word;background-color:rgb(246,246,246)"><span class="forcewrap" style="word-break:break-all"><a rel="nofollow" href="https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d" style="color:rgb(102,0,102)"><br class="Apple-interchange-newline">
</a></span><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="forcewrap" style="word-break:break-all"><a rel="nofollow" href="https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d" style="color:rgb(102,0,102)">https://github.com/timotheecour/dtools/blob/master/dtools/util/variant_nested.d</a></span>
       auto<br>d=variantTupleNamed("a",1,"b","foo","c",variantTuple(1,2.2,"three"));<br>d["a"]=2;<br>auto v=d["c"][0].get!int;//can coerce to int<br>v+=3;<br>
d["c"][0]="other1";//can access nested type<br>d["a"]="other2";//can change type<br>d["a"]=variantTuple(0.0,'e');<br>d["a"]=10;<br>d["a"]+=2; //read-modify-write works, unlike std.variant : 'Due to<br>
limitations in current language, read-modify-write operations op= will not<br>work properly'<br>assert(d.text==`["a":12, "b":foo, "c":[other1, 2.2, three]]`);
Pending DIP32 (Uniform tuple syntax), this could improve to:<br>       auto d=variant( {a=1,b="foo", c={1,2.2,"three"}} );</blockquote></pre></div><div><br></div></div>