Initalizing complex array types or some other problem ;/

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 15 13:54:48 PDT 2015


On Tuesday 15 September 2015 22:09, Prudence wrote:

> The code below doesn't work.

Please be more specific in how it doesn't work. Mention the error message if 
there is one, or say how the code behaves differently from what you'd 
expect.

Trying to compile the code (after kicking getch out), I get this error:
core.exception.RangeError at test.d(103): Range violation

Line 103 is: writeln(MyStore.Store[k].length);

I can't find where you set Store[k]. Maybe you forgot that or deleted it 
accidentally?

I made a guess and added this line in SingleStore.New:
o.Store[k] ~= tuple(v, o);

It still throws a range error with this. That's because associative arrays 
are a little weird, unfortunately.

An AA is effectively initialized on the first assignment. So (with my 
addition) the first SingleStore.New call initializes the AA. But it only 
initializes o.Store, not the original variable s (i.e. ObjectStore.Store). s 
is left empty.

Possible solutions/workarounds:
* Append to s[k], then assign s to o.Store.
* Initialize ObjectStore.Store in a static constructor:
----
    static this()
    {
        Store[TKey.init] = [];
        Store.remove(TKey.init);
    }
----

Aside: I have no idea what you're doing there, but it looks pretty 
complicated, to the point that I'd guess that it's more complicated than 
necessary.


More information about the Digitalmars-d-learn mailing list