use variant as associative array

GG g at g.com
Thu Feb 11 22:01:28 PST 2010


Yes I can't use struct and/or enum because I can't know type and number of data that user could pass.
In this example, we talk about Month, but it could be Minutes, week... So I try to program dynamic not static.

In fact I'm learning D thought a little project of graphic chart. I would like to keep a notion that I have found very usefull in AS3 : ArrayCollection.

I just want to know if it possible in D to have only one BIG collection of any type of data (int,string,bool,double...) ?

We talk about Variant but it could be other ? Maybe not...

So I tried this :
        Variant[char[]][int] aa;

	aa[0]["Month"] = Variant("Jan"); // compile
	aa[0]["Profit"] = Variant(500); // compile

	aa[1]["Month"] = Variant("Feb");  // compile
	aa[1]["Profit"] = Variant(800); // compile
	
	for(int i=0;i<aa.length;i++) // compile failed on aa.length and get dmd: mtype.c:3426: StructDeclaration* TypeAArray::getImpl(): Assertion `impl' failed.
	{
		writefln("%s",aa[i]["Month"]); // compile but get at runtime : core.exception.RangeError at test(25): Range violation
	}
It seems to be a bug...

Thanks  !


More information about the Digitalmars-d-learn mailing list