I wonder if it's possible to modify some value of Optional without making a copy. The specific case where this comes up is conditionally creating a new value in Dictionary or appending to the current value.
This doesn't work because array gets copied:
switch dict[key] {
case .none:
dict[key] = [value]
case .some(var array):
array.append(value)
}
However the working version has ! in it which reviewers have to spend time on checking if it can fail. One could use ? instead but then it'd silently stop appending if there's a mistake (during refactoring) and would be a nightmare to debug.
In other words, I wonder if it's possible to to directly translate this Rust code:
As with some other questions around the use of _modify, the admittedly opaque incantation &self! is carrying a lot of load here. This convinces Swift to elide a copy into a temporary when it passes the inner value into the function argument. I'm honestly not 100% that this works in all edge cases, but so far this has worked in all situations where I've needed to use it.