<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>I did not mind sending messages to nil being essentially a NoOp so calling map on an optional and have the function only execute if the value included inside the optional is not nil is kind of reminding of that ;). Although it is a lot wordier and not really in line with how we force people to deal with nil in then general case --&gt; you better unwrap or use optional chaining.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">It feels almost like this use of map is something that should come in one of those web adverts "See the 10 things Apple does not want you to do with Optionals, but made these coders £40,000" ;).<br><br>Sent from my iPhone</div><div><br>On 12 Jul 2016, at 17:37, Jordan Rose via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><div class="">Hi, Liam. The particular issue we’ve seen when exploring this feature is that it’s unclear whether or not other arguments get evaluated:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">print(a.foo()?, b.bar()?, c.baz()?)</div></blockquote><div class=""><br class=""></div><div class="">If b.bar() turns out to be nil, does c.baz() still get evaluated?</div><div class=""><br class=""></div><div class="">(I’m pretty sure the right answer is “yes”, but it’s still something that should be addressed explicitly.)</div><div class=""><br class=""></div><div class="">The added complexity and the existence of Optional.map led us not to pursue this direction in the past.</div><div class=""><br class=""></div><div class="">Jordan</div><br class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jul 12, 2016, at 07: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">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></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></body></html>