use variant as associative array

Daniel Keep daniel.keep.lists at gmail.com
Thu Feb 11 17:34:06 PST 2010


GG wrote:
> I try to use variant as associative array with dmd2.039, 
> 
> Variant[char[]][int] aa;
> aa[0]["Month"] = "Jan";
> aa[0]["Profit"] = 500;
> aa[1]["Month"] = "Feb";
> aa[1]["Profit"] = 800;
> 
> I got core.exception.RangeError at main(28): Range violation
> I'm doing something wrong ? Or it's same problem as :
> http://digitalmars.com/d/archives/digitalmars/D/Variant_string_associative_array_..._fail_87248.html
> Same problem with dmd2.040
> 
> Thanks !
> GG

I got the following to work with Tango's Variant (dmd 1.051 + pre-0.99.9
trunk):

    Variant[char[]][int] aa;

    aa[0] = typeof(aa[0]).init;
    aa[0]["Month"] = Variant("Jan");
    aa[0]["Profit"] = Variant(500);

    aa[1] = typeof(aa[0]).init;
    aa[1]["Month"] = Variant("Feb");
    aa[1]["Profit"] = Variant(800);

There's actually two problems at work here:

1. You can't use a nested associative array without initialising it.

2. You apparently can't assign to an aa of Variants for some reason.

Keep in mind that Variants are basically a hack on the language; that
assigning to a Variant value in an AA doesn't work isn't entirely
surprising.


More information about the Digitalmars-d-learn mailing list