digitalmars.D - When will map/dict initialization be ready?
- John Xu (12/12) Apr 05 2023 I saw on doc:
- Paul Backus (22/34) Apr 05 2023 Specifically, what doesn't work is initializing an associative
- John Xu (2/11) Apr 06 2023 Ok, great, thanks. Better add to the online document.
- Steven Schveighoffer (18/32) Apr 06 2023 You can get almost the same thing via a library type that duplicates the...
I saw on doc:
https://dlang.org/spec/hash-map.html#static_initialization,
that map/dict/associative array, still can't be initialized like
below:
NOTE: Not yet implemented.
immutable long[string] aa = [
"foo": 5,
"bar": 10,
"baz": 2000
];
I'm wondering when this is be ready? Python can write this way
long time ago.
Apr 05 2023
On Thursday, 6 April 2023 at 01:53:31 UTC, John Xu wrote:
I saw on doc:
https://dlang.org/spec/hash-map.html#static_initialization,
that map/dict/associative array, still can't be initialized
like below:
NOTE: Not yet implemented.
immutable long[string] aa = [
"foo": 5,
"bar": 10,
"baz": 2000
];
I'm wondering when this is be ready? Python can write this way
long time ago.
Specifically, what doesn't work is initializing an associative
array *at compile time*. You can use this syntax to initialize an
AA at runtime, and it will work just fine.
For example:
// Error - global variables are initialized at compile time
long[string] globalAa = ["foo": 5, "bar": 10];
void main()
{
// No error - local variables are initialized at runtime
long[string] localAa = ["baz": 2000, "quux": 9999];
}
To work around this limitation, you can initialize a global AA
using a [static module constructor][1]. For example:
long[string] globalAa;
static this()
{
globalAa = ["foo": 5, "bar": 10];
}
This will perform the initialization at runtime, during program
startup (before calling `main`).
[1]: https://dlang.org/spec/module.html#staticorder
Apr 05 2023
On Thursday, 6 April 2023 at 02:49:55 UTC, Paul Backus wrote:
To work around this limitation, you can initialize a global AA
using a [static module constructor][1]. For example:
long[string] globalAa;
static this()
{
globalAa = ["foo": 5, "bar": 10];
}
This will perform the initialization at runtime, during program
startup (before calling `main`).
Ok, great, thanks. Better add to the online document.
Apr 06 2023
On 4/5/23 9:53 PM, John Xu wrote:I saw on doc: https://dlang.org/spec/hash-map.html#static_initialization, that map/dict/associative array, still can't be initialized like below: NOTE: Not yet implemented. immutable long[string] aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; I'm wondering when this is be ready? Python can write this way long time ago.You can get almost the same thing via a library type that duplicates the layout of the runtime type. I did so in my `newaa` library: https://code.dlang.org/packages/newaa e.g.: ```d immutable Hash!(long, string) aa = [ "foo": 5, "bar": 10, "baz": 2000 ]; ``` The usage is a bit rough around the edges, I haven't implemented everything that is possible. But indexing works, should work for your use case. But it can be cast to an AA using `asAA` (not sure about immutable support, I didn't test it too much). -Steve
Apr 06 2023









John Xu <728308756 qq.com> 