How to make a static immutable associative array using assocArray in CT?
monkyyy
crazymonkyyy at gmail.com
Tue Feb 10 22:47:12 UTC 2026
On Tuesday, 10 February 2026 at 21:52:52 UTC, realhet wrote:
> On Tuesday, 10 February 2026 at 21:14:29 UTC, monkyyy wrote:
>> On Tuesday, 10 February 2026 at 20:46:11 UTC, realhet wrote:
>>> Hi,
>>
>>
>> ```d
>> enum int[string] foo=(){
>> int[string] o;
>> o["hi"]=2;
>> o["bye"]=3;
>> o["hi"]=4;
>> return o;
>> }();
>> ```
>
> ```d
> static immutable foo = ["this":2, "works":3, "fine":4];
> ```
>
> The problem occurs when I do it with assocArray() function and
> giving it the key and value ranges from an enum.
>
> I want to automatize this dictionary because the contents of
> that enum will be changed/maintained later.
```d
import std;
enum SysType {Entity, String, Int, Bool, Float, Double, DateTime,
Date, Time}
enum stringofenum(alias T)=(){
string[T] o;
static foreach(t;__traits(allMembers, T)){
o[mixin("T."~t)]=t;
}
return o;
}();
unittest{
stringofenum!SysType.writeln;
}
static immutable foo=stringofenum!SysType;
unittest{
foo.writeln;
}
```
More information about the Digitalmars-d-learn
mailing list