<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 13, 2015, at 2:21 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class=""><div class="">A function I find myself defining in a lot of my projects looks like the following:<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">/// Replace the value of `a` with `b` and return the old value.<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">public func replace<T>(inout a: T, with b: T) -> T {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> var value = b<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> swap(&a, &value)<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> return value<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}<br class=""></span></div>
<div class=""> </div>
<div class="">This is a pretty simple function, and useful in a wide variety of circumstances, so I'd love to get it into the standard library. It doesn't actually enable any behavior that wasn't previously possible, but it does shrink some common code patterns, and I find the shorter code easier to read.<br class=""></div>
<div class=""> </div>
<div class="">An example of a place where I use it often is in replacing an optional property with a new value (or with nil) and cleaning up the previous value. Assuming a property like<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">var task: NSURLSessionTask?<br class=""></span></div>
<div class=""> </div>
<div class="">This replaces<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">if let task = self.task {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> task.cancel()<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">task = nil<br class=""></span></div>
<div class=""> </div>
<div class="">with<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">replace(&task, with: nil)?.cancel()<br class=""></span></div>
<div class=""> </div>
<div class="">Or sometimes I use it like<br class=""></div>
<div class=""> </div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">if let value = replace(&prop, with: newValue) {</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"> // multi-line cleanup</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;">}</span><span class="font" style="font-family: menlo, consolas, "courier new", monospace, sans-serif;"><br class=""></span></div>
<div class=""> </div>
<div class="">This is particularly nice if it's a COW value that I want to mutate, as it means I don't have to worry about getting unwanted copies due to the property still holding the old value while I muck with it.<br class=""></div></div></div></blockquote><div><br class=""></div><div>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 library. 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.</div><div><br class=""></div><div>If cases other than "nondestructive move" are common <u class="">enough</u>, I'd consider something like this syntax instead:</div><div><br class=""></div><div> (task <- nil).cancel()</div><div><br class=""></div><div>But I strongly suspect this isn't a common enough pattern to warrant introducing an operator.</div><div><br class=""></div><blockquote type="cite" class=""><div class="">
<div class="">Question: For trivial backwards-compatible API changes like this, does a proposal PR need to be submitted to the swift-evolution repo, or is discussion on this ML sufficient before submitting a patch?</div></div></blockquote><br class="">A proposal is needed for all new/changed API.</div><div><br class=""></div><div>-Dave</div></body></html>