TIL: statically initializing an Associative Array

Steven Schveighoffer schveiguy at gmail.com
Tue May 7 01:14:24 UTC 2024


On Tuesday, 7 May 2024 at 00:10:27 UTC, Andy Valencia wrote:
> I had a set of default error messages to go with error code 
> numbers, and did something along the lines of:
>
> string[uint] error_text = [
>     400: "A message",
>     401: "A different message"
> ];
>
> and got "expression .... is not a constant"
>
> I eventually found this discussion:
>     https://issues.dlang.org/show_bug.cgi?id=6238
>
> I understand that it's problematic, but a message which makes 
> it clearer that compile-time initialization of global AA's are 
> not supported?  Because it cost me about a half hour trying to 
> figure out what I was doing wrong.

This error message was changed in 2.101.x (unsure which point 
release):

```
onlineapp.d(1): Error: static initializations of associative 
arrays is not allowed.
onlineapp.d(1):        associative arrays must be initialized at 
runtime: 
https://dlang.org/spec/hash-map.html#runtime_initialization
```

>
> (My workaround was to initialize the data structure once during 
> app startup.)

This was fixed [in 
2.106.0](https://dlang.org/changelog/2.106.0.html#dmd.static-assoc-array)

  please upgrade your compiler.

-Steve


More information about the Digitalmars-d-learn mailing list