Initializing an associative array of struct

Stanislav Blinov stanislav.blinov at gmail.com
Sun Jun 14 09:34:08 UTC 2020


On Sunday, 14 June 2020 at 04:36:09 UTC, Denis wrote:

> Note also that the defaults for id and value are fine...

> I would welcome a suggestion for how to initialize the keys of 
> parameters. As there will be a couple dozen of the param string 
> keys, a more succinct method would be preferable over a verbose 
> one.

Param[string] parameters;
string[] keys =
[
"huh", "buh", "hrm", "pff", "err", "ack", "ugh",
/* ... */
"zzz"
];
foreach (k; keys)
     parameters.require(k);

https://dlang.org/spec/hash-map.html#inserting_if_not_present

The `require` function, when called only with key, will insert 
default (.init) value for that key.


More information about the Digitalmars-d-learn mailing list