www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Re: use variant as associative array

reply GG <g g.com> writes:
I tried your trick but I got "Error: cannot append type string to type
VariantN!(maxSize)[]" 
on :  vars ~= "Jan";

I found this : http://d.puremagic.com/issues/show_bug.cgi?id=3692, the issue
has been REOPEN.

Thanks for helping !
Feb 11 2010
next sibling parent GG <g g.com> writes:
Sorry, but I ckicked on REPLY and it created a new thread after posting!?
Feb 11 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--00151758aa6ce3a92e047f59cfbd
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Feb 11, 2010 at 21:22, GG <g g.com> wrote:

 I tried your trick but I got "Error: cannot append type string to type
 VariantN!(maxSize)[]"
 on :  vars ~= "Jan";

 I found this : http://d.puremagic.com/issues/show_bug.cgi?id=3692, the
 issue has been REOPEN.

 Thanks for helping !

Hmm, it worked for me, but that was almost a year ago. Maybe I wrapped a Variant around? vars ~= Variant("Jan"); (*testing*) Yes, it works: Variant[] vars; int[char[]][int] indices; vars ~= Variant("Jan"); indices[0]["Month"] = vars.length-1; Ugly as sin, though, and cumbersome to use. But I used it to add 'properties' to nodes and edges in a graph class (like, adding weight to edges as double, and names to nodes as strings...) and it worked quite well, with a few functions to hide the trick away. But it's sad you can't use Variant directly as a value. You can use it as a key, sort of: int[Variant] test; test[Variant(3)] = 4; // test[3] = 4 does not work test[Variant("abc")] = 0; writeln(test[Variant("abc")]); // 0 auto k = test.keys; // crash! Assertion failure: 'impl' on line 3886 in file 'mtype.c' --00151758aa6ce3a92e047f59cfbd Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Thu, Feb 11, 2010 at 21:22, GG <span = dir=3D"ltr">&lt;<a href=3D"mailto:g g.com">g g.com</a>&gt;</span> wrote:<br=
<blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; bord=

I tried your trick but I got &quot;Error: cannot append type string to type= VariantN!(maxSize)[]&quot;<br> on : =A0vars ~=3D &quot;Jan&quot;;<br> <br> I found this : <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D3= 692" target=3D"_blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D3692= </a>, the issue has been REOPEN.<br> <br> Thanks for helping !<br></blockquote><div><br>Hmm, it worked for me, but th= at was almost a year ago. Maybe I wrapped a Variant around?<br><br>vars ~= =3D Variant(&quot;Jan&quot;);<br>=A0<br></div></div>(*testing*)<br><br>Yes,= it works:<br> <br>Variant[] vars;<br>int[char[]][int] indices;<br><br>vars ~=3D Variant(&= quot;Jan&quot;);<br>indices[0][&quot;Month&quot;] =3D vars.length-1;<br><br=
Ugly as sin, though, and cumbersome to use. But I used it to add &#39;prop=

es as double, and names to nodes as strings...) and it worked quite well, w= ith a few functions to hide the trick away.<br> <br>But it&#39;s sad you can&#39;t use Variant directly as a value. You can= use it as a key, sort of:<br><br>int[Variant] test;<br>test[Variant(3)] = =3D 4; // test[3] =3D 4 does not work<br>test[Variant(&quot;abc&quot;)] =3D= 0;<br> writeln(test[Variant(&quot;abc&quot;)]); // 0<br>auto k =3D test.keys; // c= rash! Assertion failure: &#39;impl&#39; on line 3886 in file &#39;mtype.c&#= 39;<br><br> --00151758aa6ce3a92e047f59cfbd--
Feb 11 2010