<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Nice idiom! Even if it doesn’t make it into stdlib, I might have to use that in some project-local utilities. Quite slick.</div><div class=""><br class=""></div><div class="">Is there a way to make it read more grammatically?</div><div class=""><br class=""></div><div class=""><div class=""><div class=""><span class="font" style="font-family: menlo, consolas, 'courier new', monospace, sans-serif;">&nbsp; &nbsp; replace(&amp;task, with: nil).andWithPreviousValue?.cancel()</span></div></div></div><div class=""><br class=""></div>Yuck. I don’t see a way that works well … but you see what I’m getting at.<br class=""><div class=""><br class=""></div><div class="">P</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 13, 2015, at 4:21 PM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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="">&nbsp;</div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, 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, &quot;courier new&quot;, monospace, sans-serif;">public func replace&lt;T&gt;(inout a: T, with b: T) -&gt; T {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; var value = b<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; swap(&amp;a, &amp;value)<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp;&nbsp;&nbsp; return value<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}<br class=""></span></div>
<div class="">&nbsp;</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="">&nbsp;</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="">&nbsp;</div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">var task: NSURLSessionTask?<br class=""></span></div>
<div class="">&nbsp;</div>
<div class="">This replaces<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">if let task = self.task {<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; task.cancel()<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}<br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">task = nil<br class=""></span></div>
<div class="">&nbsp;</div>
<div class="">with<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">replace(&amp;task, with: nil)?.cancel()<br class=""></span></div>
<div class="">&nbsp;</div>
<div class="">Or sometimes I use it like<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">if let value = replace(&amp;prop, with: newValue) {</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">&nbsp; &nbsp; // multi-line cleanup</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class=""><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;">}</span><span class="font" style="font-family: menlo, consolas, &quot;courier new&quot;, monospace, sans-serif;"><br class=""></span></div>
<div class="">&nbsp;</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 class="">&nbsp;</div>
<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 class="">&nbsp;</div>
<div class="">-Kevin Ballard</div>

<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=zCg-2FSGF9Wk188a6c55kLyEbrj7YhaXxFEHM-2F-2B0YAlzUThwHxTpf8H0fARnBd8Lk0OhuULwH9wMy8ZJ0p-2BCN2q8gIIVvfRT0CGoXdDK-2BF-2B91SyKMQH8azl1DfZkpNSxzlE85y9U5WCLqpganJ8F7nIue0mCJutMmFdSQJBoT-2FV1QZOcHJVe1sFBCGkrs121wtTgFOaV08TvmlA8cA7aCVBT37g5nhuRmJcZFIZrk7agw-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>


_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>