<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><div></div><div>I'm glad that you agree with me. However, my proposal has been marked as out of scope for swift 3.0, so we'll have to wait a month or so to bring this back up again. </div><div><br>On Jul 13, 2016, at 10:24 AM, <a href="mailto:charles@charlesism.com">charles@charlesism.com</a> <<a href="mailto:charlesism.com@gmail.com">charlesism.com@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div>+1</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">I'm in favour of this, or something similar. It currently requires a bit more effort to deal with an optional that appears as an argument. It would be a bit nicer to make decisions about control flow when the amount of effort your choices require aren't affected by things like this.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature"><br><br>Sent from my iPhone</div><div><br>On Jul 12, 2016, at 7:16 AM, Liam Stevenson via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div>Optional chaining is one of the great, useful features of Swift. It can be used <span style="background-color: rgb(255, 255, 255);" class=""><font color="#333333" class="">“for querying and calling properties, methods, and subscripts on an optional that might currently be nil,” to quote Apple's "The Swift Programming Language.” However, often it is </font></span><font color="#333333" class="">necessary to call a function, subscript, or initializer conditionally based on if one or more parameters are nil. </font><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255);" class="">The proposed solution is to allow a question mark (?) to be placed after an optional value wished to be used as a parameter. Then, the function, initializer, or subscript will be called if and only if the parameter's value is not nil</span><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255);" class="">. If it has a return type, it will return an optional, which will be nil</span><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255);" class=""> if the parameter is nil.</span><div class=""><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255);" class=""><br class=""></span></div><div class=""><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255);" class="">Old way (with seemingly unnecessary if statement considering the flexibility provided by optional chaining):</span></div><div class=""><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><font face="Helvetica" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var arr = ["apples", "oranges", "pears", "bananas"]
<span class="Apple-tab-span" style="white-space:pre">        </span>let index: Int? = 2
<span class="Apple-tab-span" style="white-space:pre">        </span>var removedElement: String?
<span class="Apple-tab-span" style="white-space:pre">        </span>if let index = index {
<span class="Apple-tab-span" style="white-space:pre">                </span>removedElement = arr.removeAtIndex(index) //sets removedElement to "pears"
<span class="Apple-tab-span" style="white-space:pre">        </span>}</font></pre><div class="">Using this proposal:</div></div><div class=""><pre style="word-wrap: break-word;" class=""><font face="Helvetica" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><span style="white-space: pre-wrap;" class="">var arr = ["apples", "oranges", "pears", "bananas"]
</span><span class="Apple-tab-span" style="white-space: pre;">        </span><span style="white-space: pre-wrap;" class="">let index: Int? = 2
</span><span class="Apple-tab-span" style="white-space: pre;">        </span><span style="white-space: pre-wrap;" class="">var removedElement: String?
</span><span class="Apple-tab-span" style="white-space: pre;">        </span><span style="white-space: pre-wrap;" class="">removedElement = arr.removeAtIndex(index?) //sets removedElement to “pears"</span></font></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><font face="Helvetica" class="">Another similar example:</font></pre><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><div style="white-space: normal;" class=""><span style="color: rgb(51, 51, 51); background-color: rgb(255, 255, 255);" class=""><font face="Helvetica" class="">Old way:</font></span></div><div style="white-space: normal;" class=""><pre style="word-wrap: break-word; white-space: pre-wrap;" class=""><font face="Helvetica" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>var arr = ["apples", "oranges", "pears", "bananas"]
<span class="Apple-tab-span" style="white-space: pre;">        </span>let index: Int? = nil
<span class="Apple-tab-span" style="white-space: pre;">        </span>var removedElement: String?
<span class="Apple-tab-span" style="white-space: pre;">        </span>if let index = index {
<span class="Apple-tab-span" style="white-space: pre;">                </span>removedElement = arr.removeAtIndex(index) //never called
<span class="Apple-tab-span" style="white-space: pre;">        </span>}</font></pre><div class=""><font face="Helvetica" class="">Using this proposal:</font></div></div><div style="white-space: normal;" class=""><pre style="word-wrap: break-word;" class=""><font face="Helvetica" class=""><span class="Apple-tab-span">        </span><span style="white-space: pre-wrap;" class="">var arr = ["apples", "oranges", "pears", "bananas"]
</span><span class="Apple-tab-span">        </span><span style="white-space: pre-wrap;" class="">let index: Int? = nil
</span><span class="Apple-tab-span">        </span><span style="white-space: pre-wrap;" class="">var removedElement: String?
</span><span class="Apple-tab-span">        </span><span style="white-space: pre-wrap;" class="">removedElement = arr.removeAtIndex(index?) //removeAtIndex is never called, and removedElement is set to nil</span></font></pre></div><div class=""><font face="Helvetica" class=""><br class=""></font></div></pre><div class="">What does everyone think of this proposal? It is additive so it will not break any existing code, and in the future it will provide conciseness and clarity since the syntax is similar to the existing optional chaining syntax.</div><div class=""><br class=""></div><div class="">View the full proposal on GitHub here: <a href="https://github.com/liam923/swift-evolution/blob/master/proposals/NNNN-extend-optional-chaining-to-function-initializer-and-subscript-parameters.md" class="">https://github.com/liam923/swift-evolution/blob/master/proposals/NNNN-extend-optional-chaining-to-function-initializer-and-subscript-parameters.md</a></div></div><div class=""><br class=""></div><div class="">Liam</div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></blockquote></div></body></html>