<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">what is the code</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><i>var a = 1</i></div><div class="gmail_default" style="font-family:georgia,serif"><i>var b = -1</i></div><div class="gmail_default" style="font-family:georgia,serif"><i>var c = replace(&a, with: b</i>)</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">different from</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><div class="gmail_default"><i>var a = 1</i></div><div class="gmail_default"><i>var b = -1</i></div><div class="gmail_default"><i>var c = a</i></div><div class="gmail_default"><i>a = b</i></div><div class="gmail_default"><br></div><div class="gmail_default">?</div><div class="gmail_default"><br></div><div class="gmail_default">For me, the latter is more clearer.</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 10, 2016 at 8:48 AM, Kevin Ballard via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><u></u>
<div><div>Proposal PR submitted as <a href="https://github.com/apple/swift-evolution/pull/93" target="_blank">https://github.com/apple/swift-evolution/pull/93</a><br></div>
<div> </div>
<div>-Kevin Ballard<br></div>
<div> </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 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 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 style="font-family:menlo,consolas,'courier new',monospace,sans-serif"> var value = b</span><br></div>
<div><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif"> swap(&a, &value)</span><br></div>
<div><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif"> return value</span><br></div>
<div><span 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 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 style="font-family:menlo,consolas,'courier new',monospace,sans-serif">if let task = self.task {</span><br></div>
<div><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif"> task.cancel()</span><br></div>
<div><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif">}</span><br></div>
<div><span 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 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 style="font-family:menlo,consolas,'courier new',monospace,sans-serif">if let value = replace(&prop, with: newValue) {</span><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif"></span><br></div>
<div><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif"> // multi-line cleanup</span><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif"></span><br></div>
<div><span style="font-family:menlo,consolas,'courier new',monospace,sans-serif">}</span><span 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> </div>
<img alt="" width="1" height="1" border="0" style="min-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">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><br>Owen Zhao<br></div></div></div>
</div>