XO0532: a Map value read, changed, and stored back
Reported by xo vet, not xo check. var x = m.get(k) ?? d, statements that only change x, then m[k] = x looks the key up twice and depends on the last line: values are copies (core 2.6), so without it the change is lost (the checker’s XO0520). m.update(k, d, fn(var x) { ... }) reads k (or d), runs the function on the value in place, stores it, and returns the function’s value (std.md 2.4). The fix rewrites the statements into that call. See decision 0266.
Example
fn index(var by: Map[Str, List[Int]], name: Str, i: Int) {
var ids = by.get(name) ?? []
ids.push(i)
by[name] = ids
}Fix
fn index(var by: Map[Str, List[Int]], name: Str, i: Int) {
by.update(name, [], fn(var ids) {
ids.push(i)
})
}Run xo explain XO0532 for this text in a terminal, or see
section 10.6 of the specification.