<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Probably I wasn't clear about that. Sorry.</div><div><br></div><div>My concern was about adding (code from Dave Sweeris):</div><div><br></div><div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">extension</span>&nbsp;<span class="">Array</span>&nbsp;<span class="">where</span>&nbsp;Element: NilLiteralConvertible {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;<span class="">subscript</span>(ifExists idx:&nbsp;<span class="">Index</span>) -&gt;&nbsp;<span class="">Element</span>? {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">get</span>&nbsp;{&nbsp;<span class="">return</span>&nbsp;(<span class="">startIndex</span>&nbsp;..&lt;&nbsp;<span class="">endIndex</span>) ~= idx ?&nbsp;<span class="">self</span>[idx] :&nbsp;<span class="">nil</span>&nbsp;}</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">set</span>&nbsp;{&nbsp;<span class="">if</span>&nbsp;(<span class="">startIndex</span>&nbsp;..&lt;&nbsp;<span class="">endIndex</span>) ~= idx {&nbsp;<span class="">self</span>[idx] = newValue ?? Element(nilLiteral: ())} }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp;&nbsp;}</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">}</span></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">Since it would allow this:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = nil // sets array[0] to nil if index is valid</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;">array = [1]</div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] =&nbsp;array[ifExists: 1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">Whereas the normal behavior:</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = nil // does nothing</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[1]"</span></div><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] =&nbsp;array[ifExists: 1] // does nothing</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">print(array) // "[1]"</span></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">Hope this clarifies my point</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);">- Maximilian</span></div></div><div><br>Am 06.02.2016 um 21:31 schrieb Thorsten Seitz &lt;<a href="mailto:tseitz42@icloud.com">tseitz42@icloud.com</a>&gt;:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div></div><div><br></div><div><br>Am 06.02.2016 um 00:58 schrieb Maximilian Hünenberger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div></div><div>You are totally right. The return type is "Int??".</div><div><br></div><div>My point was that if we allowed something like this (as suggested by Dave Sweeris I think):</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; var array: [Int?] = [1]</div><div>&nbsp; &nbsp; &nbsp; &nbsp; array[ifExists: 0] = nil</div><div><br></div><div>To set the element at index 0 to nil instead of doing nothing.</div><div>The next example would also set index 0 to nil even though the getter failed:</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] =&nbsp;array[ifExists: 1]</span></div></div></blockquote><div><br></div>No, it doesn't. Just try it out.<div><br></div><div>-Thorsten&nbsp;</div><div><br></div><div><br><blockquote type="cite"><div><div><br></div><div><br></div><div>- Maximilian</div><div><br>Am 05.02.2016 um 10:20 schrieb Haravikk &lt;<a href="mailto:swift-evolution@haravikk.me">swift-evolution@haravikk.me</a>&gt;:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><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></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></div></blockquote></body></html>