Rang violation using AA's

simendsjo simendsjo at gmail.com
Fri Feb 8 11:24:54 PST 2013


On Friday, 8 February 2013 at 17:16:15 UTC, Nrgyzer wrote:
> Hi guys,
>
> I'm updated from DMD 2.060 to 2.061 and I just run into some 
> trouble by using associative arrays. Let's say I've the 
> following few lines:
>
> string[string] myValues;
>
> ref string getValue(string v) {
>    return myValues[v];
> }
>
> void main() {
>    getValue("myValue") = "myString";
> }
>
> I get a range violation in getValue() because the entry does 
> not exist. But as far as I know, this worked in 2.060 (no range 
> violation). One idea was to initialize the value in getValue() 
> if it does not exist, but my associative array looks like:
>
> string[string][][string] myValues;
>
> ... how to initialize this? So, is this a bug or is it my 
> mistake?

Did that really work before? Looks like a bug as you are 
accessing an element that doesn't exist.

The following works:

import std.stdio;
void main() {
     string[string][][string] myValues;
     assert(myValues.length == 0);

     myValues["a"] = new string[string][100]; // Doesn't have to 
create 100 elements of course
     assert(myValues["a"].length == 100);
     assert(myValues["a"][0].length == 0);

     myValues["a"][0]["b"] = "aoeu";
     assert(myValues["a"][0]["b"] == "aoeu");
}


More information about the Digitalmars-d-learn mailing list