www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Cannot initialize associative array

reply rumbu <rumbu rumbu.ro> writes:
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.
Jan 19 2018
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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 ]; }
Jan 19 2018
parent reply rumbu <rumbu rumbu.ro> writes:
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.
Jan 19 2018
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Jan 20 2018
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
Jan 21 2018