Cannot initialize associative array

Adam D. Ruppe destructionator at gmail.com
Fri Jan 19 23:27:06 UTC 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
   ];
}


More information about the Digitalmars-d-learn mailing list