<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="">I realize that it can be done in that way, and also in others, such as using if statements. The point of this proposal is for conciseness and simplicity. Also, doing it that way would become extensive if there were more than one parameters.<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 12, 2016, at 12:14 PM, David Hart &lt;<a href="mailto:david@hartbit.com" class="">david@hartbit.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">We can already do this nicely with map and flatMap:<div class=""><div class=""><div class=""><br class=""></div><div class="">var removedElement = index.map { arr.removeAtIndex($0) }</div></div><div class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 12 Jul 2016, at 16:16, Liam Stevenson 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=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Optional chaining is one of the great, useful features of Swift. It can be used&nbsp;<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&nbsp;</font></span><font color="#333333" class="">necessary to call a function, subscript, or initializer conditionally based on if one or more parameters are nil.&nbsp;</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="">&nbsp;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>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></div></div></blockquote></div><br class=""></div></body></html>