digitalmars.D.learn - Initializing an associative array into a variable when it is created
- Alexander Zhirov (12/12) Jul 15 2023 I still don't understand how to make this entry correct. I have a
- Danilo (14/14) Jul 15 2023 Works fine, if you add a semicolon at the end.
- Alexander Zhirov (7/8) Jul 16 2023 I'm sorry. I didn't put the question quite correctly. Yes, this
- Danilo (37/39) Jul 16 2023 Looks like this is not implemented yet:
- Alexander Zhirov (15/17) Jul 16 2023 Alternatively, i can think about your proposal. At the moment, I
- Danilo (10/12) Jul 15 2023 Sorry, forgot that part.
I still don't understand how to make this entry correct. I have a static array that I want to use exactly as an array (the structure doesn't quite fit): ```d string[][string] arr = [ "one": ["abc", "def"], "two": ["ghi", "jkl"], "three": ["mno", "pqr"] ] ``` There are the same number of elements everywhere (in the internal array).
Jul 15 2023
Works fine, if you add a semicolon at the end. ```d import std.stdio; void main() { string[][string] arr = [ "one": ["abc", "def"], "two": ["ghi", "jkl"], "three": ["mno", "pqr"] ]; writeln(arr); writeln(arr["two"]); writeln(arr["two"][0]); } ```
Jul 15 2023
On Saturday, 15 July 2023 at 23:34:22 UTC, Danilo wrote:Works fine, if you add a semicolon at the end.I'm sorry. I didn't put the question quite correctly. Yes, this is how the array is initialized. I'm trying to describe it all in a class. I.e. I need to create a variable in the class that will be initialized when creating an object. I understand that it needs to be described in the constructor. But is there really no other way to immediately point a static array to a variable?
Jul 16 2023
But is there really no other way to immediately point a static array to a variable?Looks like this is not implemented yet: - https://dlang.org/spec/hash-map.html#static_initialization Would a static constructor be okay? This way the static data is not initialized at every `new` and object creation is faster. - https://dlang.org/spec/class.html#static-constructor ```d import std.stdio; class C { static string[][string] arr; static this() { // static constructor arr = [ "one": ["abc", "def"], "two": ["ghi", "jkl"], "three": ["mno", "pqr"] ]; } this() { // object constructor } void show(string value) { writeln( value, ": ", arr[value], ": ", arr[value][0], ", ", arr[value][1] ); } } void main() { auto var = new C; var.show("one"); var.show("two"); var.show("three"); } ```
Jul 16 2023
On Sunday, 16 July 2023 at 11:16:55 UTC, Danilo wrote:Would a static constructor be okay? This way the static data is not initialized at every `new` and object creation is faster.Alternatively, i can think about your proposal. At the moment, I have solved my problem using the following method: ```d property string[][string] arr() { return [ "one": ["abc", "def"], "two": ["ghi", "jkl"], "three": ["mno", "pqr"] ]; } ``` I don't know how true it will be considered. After all, the function creates an array every time it is called, which is not correct.
Jul 16 2023
On Saturday, 15 July 2023 at 23:24:27 UTC, Alexander Zhirov wrote:There are the same number of elements everywhere (in the internal array).Sorry, forgot that part. Just add the size of the internal array (2 in this case): ```d string[2][string] arr = [ "one": ["abc", "def"], "two": ["ghi", "jkl"], "three": ["mno", "pqr"] ]; ```
Jul 15 2023