How to initialize a associative array?

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 23 17:06:06 PST 2016


On Saturday, 24 December 2016 at 00:57:04 UTC, Yuxuan Shui wrote:
> On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui 
> wrote:
>> I tried this:
>>
>>     immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 
>> 'P':4];
>>
>> And got a "non-constant expression" error (with or without 
>> 'immutable').
>>
>> What's the correct way?
>
> This example here: https://dlang.org/spec/hash-map.html, 
> doesn't work either.

Is this at global scope?

You need to use a `shared static this() { ... }` to initialise it.


More information about the Digitalmars-d-learn mailing list