Cannot initialize associative array

Jacob Carlborg doob at me.com
Sun Jan 21 17:41:50 UTC 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


More information about the Digitalmars-d-learn mailing list