Rang violation using AA's
Nrgyzer
nrgyzer at unknownMailAddress.com
Fri Feb 8 12:10:31 PST 2013
On Friday, 8 February 2013 at 19:24:55 UTC, simendsjo wrote:
> 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");
> }
I downloaded 2.060, tried my example above and it works in 2.060
without any range exceptions. Using 2.061 throws me the range
exception (using the same code). I also tried the following (as
described in the documentation of AA's):
void main() {
int[string] b;
b["hello"] = 3;
}
This works for both versions... but I don't know why using a
method with ref-return value doesn't work anymore.
Thanks for your suggestion.
More information about the Digitalmars-d-learn
mailing list