update

Looks up key; if it exists applies the update callable else evaluates the create callable and adds it to the associative array

void
update
(
K
V
C
U
)
(
ref V[K] aa
,
K key
,
scope C create
,
scope U update
)
if (
is(typeof(create()) : V) &&
is(typeof(update(aa[K.init])) : V)
)

Parameters

aa
Type: V[K]

The associative array.

key
Type: K

The key.

create
Type: C

The callable to apply on create.

update
Type: U

The callable to apply on update.

Examples

1 auto aa = ["k1": 1];
2 
3 aa.update("k1", {
4     return -1; // create (won't be executed
5 }, (ref int v) {
6     return v + 1; // update
7 });
8 assert(aa["k1"] == 2);
9 
10 aa.update("k2", {
11     return 0; // create
12 }, (ref int v) {
13     return -1; // update (won't be executed)
14 });
15 assert(aa["k2"] == 0);
Suggestion Box / Bug Report