digitalmars.D.learn - Cannot initialize associative array
- rumbu (15/15) Jan 19 2018 According to this
- Adam D. Ruppe (20/23) Jan 19 2018 That only works inside a function, and, ironically, only if the
- rumbu (5/28) Jan 19 2018 Thank you Adam, just figured out myself the same solution, but I
- Jonathan M Davis (6/10) Jan 20 2018 There really isn't anything special about whatever module you have main ...
- Jacob Carlborg (7/27) Jan 21 2018 An alternative to the shared module constructor is to declare it as an
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
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
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: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.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
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
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