When will map/dict initialization be ready?

Steven Schveighoffer schveiguy at gmail.com
Thu Apr 6 14:57:01 UTC 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


More information about the Digitalmars-d mailing list