Thread overview
TIL: statically initializing an Associative Array
May 07
matheus
May 07
matheus
May 07
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.

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

May 07
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.
>
> (My workaround was to initialize the data structure once during app startup.)

Based on what I understood and that issue, I think it was fixed:

import std.stdio;

string[uint] aa1 = [1:"ABC",2:"DEF"];

void main(){
    auto aa2 = ['A':1,'B':2];

    writeln(aa1[1]);
    writeln(aa1[2]);
    writeln(aa2['A']);
    writeln(aa2['B']);
}

Prints:

ABC
DEF
1
2

Matheus.
May 07
On Tuesday, 7 May 2024 at 01:02:04 UTC, matheus wrote:
> On Tuesday, 7 May 2024 at 00:10:27 UTC, Andy Valencia wrote:
> ...
> Based on what I understood and that issue, I think it was fixed:
> ...

By the way it works as immutable too.

Matheus.


May 07

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

please upgrade your compiler.

-Steve

May 07
On Tuesday, 7 May 2024 at 01:14:24 UTC, Steven Schveighoffer wrote:
> 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"
> ...
> This was fixed [in 2.106.0](https://dlang.org/changelog/2.106.0.html#dmd.static-assoc-array)
>
>  please upgrade your compiler.

I'm using ldc2 from Debian stable; great news that it's fixed as of late 2023.  I'll probably live with my workaround, but it's good to know that it's a bug which has been resolved.

Thank you!
Andy