Initializing an associative array into a variable when it is created
Danilo
codedan at aol.com
Sun Jul 16 11:16:55 UTC 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");
}
```
More information about the Digitalmars-d-learn
mailing list