Question about the new AA behavior in 0.19

Anton V Staaf dm at socialhacker.com
Thu Aug 10 16:31:45 PDT 2006


    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



More information about the Digitalmars-d mailing list