<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>Neither code snippet appears to be doing much at all. It's hard to judge anything at all when the code has been reduced that far.<br></div>
<div> </div>
<div>-Kevin Ballard<br></div>
<div> </div>
<div>On Sat, Jan 9, 2016, at 09:53 PM, 肇鑫 wrote:<br></div>
<blockquote type="cite"><div dir="ltr"><div style="font-family:georgia, serif;">what is the code<br></div>
<div style="font-family:georgia, serif;"> </div>
<div style="font-family:georgia, serif;"><i>var a = 1</i><br></div>
<div style="font-family:georgia, serif;"><i>var b = -1</i><br></div>
<div style="font-family:georgia, serif;"><i>var c = replace(&a, with: b</i>)<br></div>
<div style="font-family:georgia, serif;"> </div>
<div style="font-family:georgia, serif;">different from<br></div>
<div style="font-family:georgia, serif;"> </div>
<div style="font-family:georgia, serif;"><div><i>var a = 1</i><br></div>
<div><i>var b = -1</i><br></div>
<div><i>var c = a</i><br></div>
<div><i>a = b</i><br></div>
<div> </div>
<div>?<br></div>
<div> </div>
<div>For me, the latter is more clearer.<br></div>
</div>
</div>
<div><div> </div>
<div><div>On Sun, Jan 10, 2016 at 8:48 AM, Kevin Ballard via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>></span> wrote:<br></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;padding-left:1ex;"><div><u></u><br></div>
<div><div>Proposal PR submitted as <a href="https://github.com/apple/swift-evolution/pull/93">https://github.com/apple/swift-evolution/pull/93</a><br></div>
<div> <br></div>
<div>-Kevin Ballard<br></div>
<div> <br></div>
<div>On Sun, Dec 13, 2015, at 02:21 PM, Kevin Ballard wrote:<br></div>
<blockquote type="cite"><div>A function I find myself defining in a lot of my projects looks like the following:<br></div>
<div> <br></div>
<div><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></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">public func replace<T>(inout a: T, with b: T) -> T {</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif"> var value = b</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif"> swap(&a, &value)</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif"> return value</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">}</span><br></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">var task: NSURLSessionTask?</span><br></div>
<div> <br></div>
<div>This replaces<br></div>
<div> <br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">if let task = self.task {</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif"> task.cancel()</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">}</span><br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">task = nil</span><br></div>
<div> <br></div>
<div>with<br></div>
<div> <br></div>
<div><span class="font" style="font-family:menlo, consolas, 'courier new', monospace, sans-serif">replace(&task, with: nil)?.cancel()</span><br></div>
<div> <br></div>
<div>Or sometimes I use it like<br></div>
<div> <br></div>
<div><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"></span><br></div>
<div><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"></span><br></div>
<div><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></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div>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></div>
<div> <br></div>
<div>-Kevin Ballard<br></div>
</blockquote><div> <br></div>
<div><img style="min-height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-bottom:0px !important;margin-right:0px !important;margin-left:0px !important;padding-top:0px !important;padding-bottom:0px !important;padding-right:0px !important;padding-left:0px !important;" border="0" height="1" width="1" alt="">
<br></div>
</div>
<div> </div>
<div>_______________________________________________<br></div>
<div>
swift-evolution mailing list<br></div>
<div>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
<div> </div>
</blockquote></div>
<div> </div>
<div> </div>
<div> </div>
<div>-- <br></div>
<div><div dir="ltr"><div><div> </div>
<div>Owen Zhao<br></div>
</div>
</div>
</div>
</div>
</blockquote><div> </div>
</body>
</html>