Cannot initialize associative array

rumbu rumbu at rumbu.ro
Fri Jan 19 23:39:08 UTC 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.



More information about the Digitalmars-d-learn mailing list