<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=""><div class="">-1 from me as well, for the same reason as Dave. In the Dictionary case, doing a lookup and then a replacement isn't just more verbose; it's actually less efficient. And I <i class="">still</i>&nbsp;wouldn't embed an updateValue(_:forKey:) in a larger expression, for clarity's sake.</div><div class=""><br class=""></div><div class="">Now, admittedly, if the assigned-to value were some complicated expression (e.g. "view.frame.size.height"), the same performance argument applies. But I still think I'd rather people just use a temporary variable.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">self.task?.cancel()</div><div class="">self.task = nil</div><div class=""><br class=""></div><div class="">let oldValue = self.prop</div><div class="">self.prop = newValue</div><div class="">if let oldValue = oldValue {</div><div class="">&nbsp; // multi-line cleanup</div><div class="">}</div></blockquote><div class=""><br class=""></div>I'll admit the latter is not as nice, but it's definitely easier for me to read.<div class=""><br class=""></div><div class="">Jordan<div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jan 9, 2016, at 16:48, 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="">Proposal PR submitted as&nbsp;<a href="https://github.com/apple/swift-evolution/pull/93" class="">https://github.com/apple/swift-evolution/pull/93</a><br class=""></div>
<div class="">&nbsp;</div>
<div class="">-Kevin Ballard<br class=""></div>
<div class="">&nbsp;</div>
<div class="">On Sun, Dec 13, 2015, at 02:21 PM, Kevin Ballard wrote:<br class=""></div>
<blockquote type="cite" 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;<br 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.</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">public func replace&lt;T&gt;(inout a: T, with b: T) -&gt; T {</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">&nbsp;&nbsp;&nbsp; var value = b</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">&nbsp;&nbsp;&nbsp; swap(&amp;a, &amp;value)</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">&nbsp;&nbsp;&nbsp; return value</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">}</span><br class=""></div>
<div class="">&nbsp;<br 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="">&nbsp;<br 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="">&nbsp;<br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">var task: NSURLSessionTask?</span><br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class="">This replaces<br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">if let task = self.task {</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">&nbsp; &nbsp; task.cancel()</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">}</span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">task = nil</span><br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class="">with<br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">replace(&amp;task, with: nil)?.cancel()</span><br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class="">Or sometimes I use it like<br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">if let value = replace(&amp;prop, with: newValue) {</span><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif"></span><br class=""></div>
<div class=""><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">&nbsp; &nbsp; // multi-line cleanup</span><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif"></span><br class=""></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"></span><br class=""></div>
<div class="">&nbsp;<br 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 class="">&nbsp;<br class=""></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?<br class=""></div>
<div class="">&nbsp;<br class=""></div>
<div class="">-Kevin Ballard<br class=""></div>
</blockquote><div class="">&nbsp;</div>

<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=ZEz4qHYnXhPr3bBPu-2FxP4tN3HfWKL-2FtJpqkQ0gkOVSAcgItFVG2AiFpQaInYzA-2BUcdEvRiMs2ybvfTanab0UsJGd6-2BymuGyDQ-2BzxnFnHtWFIISqJHAkEyLUGwue87zktBTbfJKiggADsVzrQ93w4lC5IohdUzKDT2Pe4caH0OaK0pHyxT2sGvgmmci8a1o-2F7pgsD324XUwoo8gc7MSVTzO75-2FsDaHuPUnn9UB-2FGO0FY-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>