Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 08, 2013 Rang violation using AA's | ||||
---|---|---|---|---|
| ||||
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? |
February 08, 2013 Re: Rang violation using AA's | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nrgyzer | 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");
}
|
February 08, 2013 Re: Rang violation using AA's | ||||
---|---|---|---|---|
| ||||
Posted in reply to simendsjo | 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.
|
February 08, 2013 Re: Rang violation using AA's | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nrgyzer | On Friday, 8 February 2013 at 20:10:32 UTC, Nrgyzer wrote:
> 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.
This works because an element is assigned if absent (expression is treated as lvalue). In case of return an element is not assigned (rvalue), so exception is thrown. I do not now why it worked in 2.060.
|
February 08, 2013 Re: Rang violation using AA's | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Friday, 8 February 2013 at 20:30:00 UTC, Maxim Fomin wrote:
> On Friday, 8 February 2013 at 20:10:32 UTC, Nrgyzer wrote:
>> 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.
>
> This works because an element is assigned if absent (expression is treated as lvalue). In case of return an element is not assigned (rvalue), so exception is thrown. I do not now why it worked in 2.060.
It worked in version 2.060 and lower... but as already mentioned not in 2.061. I don't know if it's intended or not.
|
Copyright © 1999-2021 by the D Language Foundation