www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Question about the new AA behavior in 0.19

reply Anton V Staaf <dm socialhacker.com> writes:
    I've just started working with D.  I'm very pleased with what I see.
 I have noticed something that might be a bug though.  I'm not sure the
correct protocol for reporting potential bugs, so I figured I'd send my
thoughts here first.

    The following code apparently worked in earlier versions of D (I
haven't tested this, but have seen similar code in other threads):

import std.stdio;

void main ()
{
    int[int][int]	array;

    array[12][15]++;
}

    I compiled this with:

    gdc test.d -o test

    And when run it results in an ArrayBoundsError exception being
thrown.  After reading the message boards I came across a change in 0.19
that I think is responsible.  Apparently associative arrays would add
entries just by being referenced.  0.19 removed this behavior and they
now throw ArrayBoundsError exceptions.  This makes perfect sense to me
and is the right behavior (at least it seems to be to me).  The problem
is that the first access in the multidimensional array modification is
being treated as just that, and access, and not an lvalue.  At least
that's what I think is happening.  This seams like an unintentional side
effect of the change.  If I change the code to initialize the inner
associative array then it works but is cumbersome:

import std.stdio;

void main ()
{
    static int[int]	init;
    int[int][int]	array;

    array[12] = init;

    array[12][15]++;
}

    That version works fine in 0.19, but means that every time you want
to modify a multidimensional associative array you need to check each
inner dimension to make sure it's initialized and initialize it if it is
not.

    Thank you,
        Anton Staaf
Aug 10 2006
next sibling parent Derek Parnell <derek nomail.afraid.org> writes:
On Thu, 10 Aug 2006 16:31:45 -0700, Anton V Staaf wrote:

...

 every time you want
 to modify a multidimensional associative array you need to check each
 inner dimension to make sure it's initialized and initialize it if it is
 not.
Yes, that is what we need to do for now. Maybe in v2.0 of D we will have some help from the compiler. And BTW, this applies to non-AA dynamic-length arrays too. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocrity!" 11/08/2006 10:37:53 AM
Aug 10 2006
prev sibling next sibling parent Sean Kelly <sean f4.ca> writes:
Anton V Staaf wrote:
     I've just started working with D.  I'm very pleased with what I see.
  I have noticed something that might be a bug though.  I'm not sure the
 correct protocol for reporting potential bugs, so I figured I'd send my
 thoughts here first.
 
     The following code apparently worked in earlier versions of D (I
 haven't tested this, but have seen similar code in other threads):
 
 import std.stdio;
 
 void main ()
 {
     int[int][int]	array;
 
     array[12][15]++;
 }
 
     I compiled this with:
 
     gdc test.d -o test
 
     And when run it results in an ArrayBoundsError exception being
 thrown.  After reading the message boards I came across a change in 0.19
 that I think is responsible.  Apparently associative arrays would add
 entries just by being referenced.  0.19 removed this behavior and they
 now throw ArrayBoundsError exceptions.  This makes perfect sense to me
 and is the right behavior (at least it seems to be to me).
Personally, I'd prefer if use of AAs as an lvalue inserted the element if it didn't exist and then returned a reference to this element, while use of AAs as an rvalue simply returned the init value of the contained type and didn't modify the AA. 'in' could still be used for testing whether the AA contained the value or not. But I suppose that's water under the bridge at this point ;-) Sean
Aug 10 2006
prev sibling next sibling parent reply nobody <nobody mailinator.com> writes:
Anton V Staaf wrote:

 thrown.  After reading the message boards I came across a change in 0.19
 that I think is responsible.  Apparently associative arrays would add
I was actually just looking for the changelog yesterday. I found: D 0.163 (current) back to D 0.140 (Nov 23, 2005) http://www.digitalmars.com/d/changelog.html D 0.139 (Nov 7, 2005) back to D 0.43 (Sep 28, 2002) http://www.digitalmars.com/d/changelog1.html However I could not find a link for anything as far back as D 0.19. I tried changelog2.html but got dropped back on the homepage. Since you were asking about D 0.19 I thought I might ask if you found anything before Sep 2002?
Aug 11 2006
parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
nobody wrote:

 Anton V Staaf wrote:
 
 thrown.  After reading the message boards I came across a change in 0.19
 that I think is responsible.  Apparently associative arrays would add
I was actually just looking for the changelog yesterday. I found: D 0.163 (current) back to D 0.140 (Nov 23, 2005) http://www.digitalmars.com/d/changelog.html D 0.139 (Nov 7, 2005) back to D 0.43 (Sep 28, 2002) http://www.digitalmars.com/d/changelog1.html However I could not find a link for anything as far back as D 0.19. I tried changelog2.html but got dropped back on the homepage. Since you were asking about D 0.19 I thought I might ask if you found anything before Sep 2002?
I suppose he meant GDC 0.19 which is based on the DMD 0.162 frontend. -- Lars Ivar Igesund blog at http://larsivi.net DSource & #D: larsivi
Aug 11 2006
parent Anton V Staaf <dm socialhacker.com> writes:
Lars Ivar Igesund wrote:
 
 I suppose he meant GDC 0.19 which is based on the DMD 0.162 frontend.
 
Yes, you are correct. I wasn't all that clear about which part I was talking about. Thanks for the info folks, Anton
Aug 11 2006
prev sibling parent S. <dnewsgr mephit.kicks-ass.org> writes:
On 2006-08-10 16:31:45 -0700, Anton V Staaf <dm socialhacker.com> said:
 
 import std.stdio;
 
 void main ()
 {
     static int[int]	init;
     int[int][int]	array;
 
     array[12] = init;
 
     array[12][15]++;
 }
The fact that you are using ++ makes this code invalid even if the symptom you state is there. (++ is a lookup and an increment.) However, I think you are correct about the multidimensional problem anyways as I have run into it in the past. I think you have to do: array[12] = new int[int]; array[12][15] = 1; for the time be-ing -S
Aug 14 2006