<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=""><div class="">There's a pretty big difference between "nil" and "Some(nil)" (and "Some(Some(1))"). This was covered pretty early on in the&nbsp;<a href="https://developer.apple.com/swift/blog/?id=12" class="">Apple Swift blog</a>&nbsp; While double optionals can be confusing for humans, the compiler ought to pretty much always do something sensible.</div><div class=""><br class=""></div><div class="">…though I did say "ought to"; the compiler has done some less-than-sensible things in overload resolution before…</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 5, 2016, at 15:58, Maximilian Hünenberger 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=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""></div><div class="">You are totally right. The return type is "Int??".</div><div class=""><br class=""></div><div class="">My point was that if we allowed something like this (as suggested by Dave Sweeris I think):</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; var array: [Int?] = [1]</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; array[ifExists: 0] = nil</div><div class=""><br class=""></div><div class="">To set the element at index 0 to nil instead of doing nothing.</div><div class="">The next example would also set index 0 to nil even though the getter failed:</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] =&nbsp;array[ifExists: 1]</span></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">- Maximilian</div><div class=""><br class="">Am 05.02.2016 um 10:20 schrieb Haravikk &lt;<a href="mailto:swift-evolution@haravikk.me" class="">swift-evolution@haravikk.me</a>&gt;:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 4 Feb 2016, at 20:24, Maximilian Hünenberger 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=""><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=""><div class="">I just realized that the normal setter for failable lookups is very nice in case of assigning/swapping:</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);"><div class="" style="margin: 0px; line-height: normal; color: rgb(211, 54, 130);">extension<span class="" style="color: rgb(147, 161, 161);">&nbsp;</span><span class="" style="color: rgb(39, 139, 210);">Array</span><span class="" style="color: rgb(147, 161, 161);">&nbsp;{</span></div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp;&nbsp;<span class="" style="color: rgb(211, 54, 130);">subscript</span>(ifExists idx:&nbsp;<span class="" style="color: rgb(39, 139, 210);">Index</span>) -&gt;&nbsp;<span class="" style="color: rgb(39, 139, 210);">Element</span>? {</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="" style="color: rgb(211, 54, 130);">get</span>&nbsp;{&nbsp;<span class="" style="color: rgb(211, 54, 130);">return</span>&nbsp;(<span class="" style="color: rgb(39, 139, 210);">startIndex</span>&nbsp;..&lt;&nbsp;<span class="" style="color: rgb(39, 139, 210);">endIndex</span>) ~= idx ?&nbsp;<span class="" style="color: rgb(211, 54, 130);">self</span>[idx] :&nbsp;<span class="" style="color: rgb(211, 54, 130);">nil</span>&nbsp;}</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="" style="color: rgb(211, 54, 130);">set</span>&nbsp;{&nbsp;<span class="" style="color: rgb(211, 54, 130);">if</span>&nbsp;(<span class="" style="color: rgb(39, 139, 210);">startIndex</span>&nbsp;..&lt;&nbsp;<span class="" style="color: rgb(39, 139, 210);">endIndex</span>) ~= idx &amp;&amp; newValue !=&nbsp;<span class="" style="color: rgb(211, 54, 130);">nil</span>&nbsp;{&nbsp;<span class="" style="color: rgb(211, 54, 130);">self</span>[idx] = newValue! } }</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; }</div><div class="" style="margin: 0px; line-height: normal;">}</div></div></div></div></blockquote></div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; // array[index1] is only set if both indexes are valid</div><div class="">&nbsp; &nbsp; &nbsp; &nbsp; array[ifExists: index1] = array[ifExists: index2]&nbsp;</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">if array is of type [Int?] and the special setter for optional Elements would have been added:</div><div class=""><br class=""></div><div class="">array[index1] would be set to "nil" if array[index2] is nil <b style="text-decoration: underline;" class="">or</b>&nbsp;index2 is not valid which is unfortunate.</div></div></div></blockquote></div><br class=""><div class="">Wouldn’t the return type be Int?? in this case? It’s not as pretty to test for as a plain Int? but iirc you can still distinguish a return type of nil from an optional that happens to contain nil, which should allow you to tell the difference between a nil value and an invalid index, I just can’t recall how at the moment (as I design around cases like these like my life depends on it ;)</div></div></blockquote></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>