use variant as associative array

Philippe Sigaud philippe.sigaud at gmail.com
Thu Feb 11 13:27:03 PST 2010


On Thu, Feb 11, 2010 at 21:22, GG <g at 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'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20100211/c202f367/attachment.htm>


More information about the Digitalmars-d-learn mailing list