CTFE and assoc array

Boris Carvajal boris2.9 at gmail.com
Sat Jan 18 21:44:35 UTC 2020


On Saturday, 18 January 2020 at 20:54:20 UTC, Andrey wrote:
> Hello,
>
> Why this doesn't work?
>
>> import std;
>> 
>> struct Qwezzz
>> {
>> 	shared static this()
>>     {
>>         qaz = qazMap;
>>     }
>> 
>>     enum qazMap = ["rrr": "vv", "hty": "4ft6"];
>>     static immutable string[string] qaz;
>> }
>> 
>> void main()
>> {
>>     enum sorted = Qwezzz.qaz.keys.sort();
>> }
>
> The variable "qaz" is static immutable and doesn't work in CTFE.

There 3 issues here:

1. "shared static this()" is a runtime construct.

2. You can't initialize a static AA right now
https://dlang.org/spec/hash-map.html#static_initialization
Only "aa = null;" works.

3. CT and RT AA internals are different.

But you can get a workaround, more info
https://forum.dlang.org/post/egrcolfiqpuplahpoiov@forum.dlang.org


More information about the Digitalmars-d-learn mailing list