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) ||
is(typeof(update(aa[K.init])) == void)
)
)

Parameters

aa V[K]

The associative array.

key K

The key.

create C

The callable to apply on create.

update U

The callable to apply on update.

Examples

auto aa = ["k1": 1];

aa.update("k1", {
    return -1; // create (won't be executed)
}, (ref int v) {
    v += 1; // update
});
assert(aa["k1"] == 2);

aa.update("k2", {
    return 0; // create
}, (ref int v) {
    v = -1; // update (won't be executed)
});
assert(aa["k2"] == 0);

Meta