How to update Associative Array?

bauss jj_1337 at live.dk
Thu Feb 10 12:04:04 UTC 2022


On Thursday, 10 February 2022 at 10:59:17 UTC, tastyminerals 
wrote:
> Not sure if the `update` method got changed but I am having 
> trouble with understanding it now.
> I assumed it would work as easy as in Python:) Just do 
> `mydic.update(dic)` or `mydic["key"].update(anotherDic)`.
>
> The docs have the following example.
>
> ```
> class C{}
> C[string] aa;
>
> C older;
> C newer;
> aa.update("a",
> {
>     newer = new C;
>     return newer;
> },
> (ref C c)
> {
>     older = c;
>     newer = new C;
>     return newer;
> });
> ```
>
> This looks pretty scary and confusing to me tbo. Also, why is 
> there an example with class and not simple `int[string]`? I 
> just need to know how can I update let's say `int[string]` or 
> nested `int[string][string]` AA. Should I also initialise and 
> additional C classes before calling this method as in the 
> example?
>
> Considering this is the only example in the docs I could find 
> on how to update AA, imagine someone from Python world comes 
> and sees this. Next thing he does, is close the page and never 
> come back to D again :(

You can just do:

aa[key] = value.

If the key exist then it's updated, if it doesn't then it's added.

The reason for the update function is simply in cases where you 
want to use the oldvalue etc. perhaps it has a timestamp that you 
need to keep etc.

But in general you shouldn't need it.


More information about the Digitalmars-d-learn mailing list