<html><body 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></body></html>