Initializing an associative array of struct

Denis noreply at noserver.lan
Sun Jun 14 14:43:51 UTC 2020


@Kagamin:

On Sunday, 14 June 2020 at 07:16:18 UTC, Kagamin wrote:
> parameters[param]=Parameter();

I did not realize that you can use a type on the RHS of an 
assignment, but it is clear and succinct. This syntax will be 
very useful going forward -- thank you.

@Stanislav B:

On Sunday, 14 June 2020 at 09:34:08 UTC, Stanislav Blinov wrote:
> string[] keys =
> [
> "huh", "buh", "hrm", "pff", "err", "ack", "ugh",
> /* ... */
> "zzz"
> ];
> foreach (k; keys)
>     parameters.require(k);

This solves the problem of initializing the keys perfectly -- 
thank you. There does not appear to be a way to loop over the 
elements of an enum, so a string array does make the most sense 
as the initial container for the keys.

I should add that your explanation for using `require` to perform 
the init is much more clear than the documentation!

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

I am just starting to learn D, and am missing so many pieces of 
the puzzle that I'm not yet able to always put "two and two" 
together myself. This kind of help really goes a long way. Again, 
my thanks to you both.

Denis


More information about the Digitalmars-d-learn mailing list