[swift-evolution] Proposal: Add replace(_:with:) function to the stdlib
kevin at sb.org
Mon Dec 14 15:26:29 CST 2015
On Sun, Dec 13, 2015, at 08:59 PM, Dave Abrahams wrote:
> This is a generalization of the postincrement pattern (mutate a value
> and return the original) and given that we're moving away from that
> I'm not sure it's something we want to enshrine in the standard
It's also a generalization of Dictionary.updateValue(_:forKey:) and
Dictionary.removeValueForKey(_:), and those aren't going away. Actually
I'd say it's a lot more like the Dictionary methods and less like
postincrement because it's not actually mutating the original value,
it's simply replacing it with a new value.
> That said, here's a question: looking at your use cases, how many of
> them are using something other than nil (or some moral equivalent) as
> the second argument? If this is effectively a non-destructive move in
> nearly all cases, I'd rather support that more directly.
In my current codebase, I have 21 uses of replace(&foo, with: nil) and 1
use of replace(&foo, with: bar). That said, in my last big Swift project
at my previous job I'm pretty sure I had more uses of replace(&foo,
with: bar) (but I don't have the source anymore to check).
> If cases other than "nondestructive move" are common _enough_, I'd
> consider something like this syntax instead:
> (task <- nil).cancel()
> But I strongly suspect this isn't a common enough pattern to warrant
> introducing an operator.
Interesting idea. I'm inclined to agree that this isn't common enough to
introduce an operator for.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-evolution