Thread overview
Cannot initialize associative array
Jan 19, 2018
rumbu
Jan 19, 2018
Adam D. Ruppe
Jan 19, 2018
rumbu
Jan 20, 2018
Jonathan M Davis
Jan 21, 2018
Jacob Carlborg
January 19, 2018
According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA:


immutable RoundingMode[string] ibmRounding =
[
    ">" : RoundingMode.towardPositive,
    "<" : RoundingMode.towardNegative,
    "0" : RoundingMode.towardZero,
    "=0": RoundingMode.tiesToEven,
    "=^": RoundingMode.tiesToAway
];


Error: non-constant expression `[">":cast(RoundingMode)2, "<":cast(RoundingMode)3, "0":cast(RoundingMode)4, "=0":cast(RoundingMode)0, "=^":cast(RoundingMode)1]`			

RoundingMode is an enum.
January 19, 2018
On Friday, 19 January 2018 at 23:16:19 UTC, rumbu wrote:
> According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA:

That only works inside a function, and, ironically, only if the variable is not `static`...

I believe this is technically an implementation shortcoming - it is supposed to work in a static context too, but it isn't implemented. But regardless, right now, you need to do it in a function (or a static constructor) right now.

You can separate declaration from initialization on module-level like so:

immutable RoundingMode[string] ibmRounding;
shared static this() {
  ibmRounding =
  [
     ">" : RoundingMode.towardPositive,
     "<" : RoundingMode.towardNegative,
     "0" : RoundingMode.towardZero,
     "=0": RoundingMode.tiesToEven,
     "=^": RoundingMode.tiesToAway
  ];
}
January 19, 2018
On Friday, 19 January 2018 at 23:27:06 UTC, Adam D. Ruppe wrote:
> On Friday, 19 January 2018 at 23:16:19 UTC, rumbu wrote:
>> According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA:
>
> That only works inside a function, and, ironically, only if the variable is not `static`...
>
> I believe this is technically an implementation shortcoming - it is supposed to work in a static context too, but it isn't implemented. But regardless, right now, you need to do it in a function (or a static constructor) right now.
>
> You can separate declaration from initialization on module-level like so:
>
> immutable RoundingMode[string] ibmRounding;
> shared static this() {
>   ibmRounding =
>   [
>      ">" : RoundingMode.towardPositive,
>      "<" : RoundingMode.towardNegative,
>      "0" : RoundingMode.towardZero,
>      "=0": RoundingMode.tiesToEven,
>      "=^": RoundingMode.tiesToAway
>   ];
> }

Thank you Adam, just figured out myself the same solution, but I didn't expect to have a static constructor in main.d. I thought static constructors are meant to be used in imported modules. Thanks again.

January 20, 2018
On Friday, January 19, 2018 23:39:08 rumbu via Digitalmars-d-learn wrote:
> Thank you Adam, just figured out myself the same solution, but I didn't expect to have a static constructor in main.d. I thought static constructors are meant to be used in imported modules. Thanks again.

There really isn't anything special about whatever module you have main in. It's just like any other module except that it has main in it. Technically, you could put main somewhere deep in your module hierarchy. It's just probably not the best idea from an organizational standpoint.

- Jonathan M Davis

January 21, 2018
On 2018-01-20 00:16, rumbu wrote:
> According to this (https://dlang.org/spec/hash-map.html#static_initialization) this is correct static initialization for AA:
> 
> 
> immutable RoundingMode[string] ibmRounding =
> [
>      ">" : RoundingMode.towardPositive,
>      "<" : RoundingMode.towardNegative,
>      "0" : RoundingMode.towardZero,
>      "=0": RoundingMode.tiesToEven,
>      "=^": RoundingMode.tiesToAway
> ];
> 
> 
> Error: non-constant expression `[">":cast(RoundingMode)2, "<":cast(RoundingMode)3, "0":cast(RoundingMode)4, "=0":cast(RoundingMode)0, "=^":cast(RoundingMode)1]`
> 
> RoundingMode is an enum.

An alternative to the shared module constructor is to declare it as an enum instead of immutable. But that will allocate a new associative array every time the enum is referenced.

enum a = [1: 2, 3: 4];

-- 
/Jacob Carlborg