Re:Map.merge() – One method to rule them all
by twic on Sunday March 31, 2019 @11:25AM
(#19536046)
The one thing that frustrates me about merge is that it returns the value inserted, rather than the value being replaced (as Map::put does).
There are good uses cases for what it does - but there are also good use cases for returning the previous value, and when i have one of those, i can't use merge, and have to fall back to a get followed by a put.
Perhaps what i would really like is a Rust-style entry API, where i could use a key to obtain a Map.Entry, which i could then inspect and mutate how i liked. It might look like:
There are good uses cases for what it does - but there are also good use cases for returning the previous value, and when i have one of those, i can't use merge, and have to fall back to a get followed by a put.
Perhaps what i would really like is a Rust-style entry API, where i could use a key to obtain a Map.Entry, which i could then inspect and mutate how i liked. It might look like:
And then i could write: To collect my prize potatoes into a map, and sell the rest. Still ugly, but hey, at least it's not Go.