<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div><br>Am 05.02.2016 um 10:20 schrieb Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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);"> </span><span class="" style="color: rgb(39, 139, 210);">Array</span><span class="" style="color: rgb(147, 161, 161);"> {</span></div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(211, 54, 130);">subscript</span>(ifExists idx: <span class="" style="color: rgb(39, 139, 210);">Index</span>) -> <span class="" style="color: rgb(39, 139, 210);">Element</span>? {</div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(211, 54, 130);">get</span> { <span class="" style="color: rgb(211, 54, 130);">return</span> (<span class="" style="color: rgb(39, 139, 210);">startIndex</span> ..< <span class="" style="color: rgb(39, 139, 210);">endIndex</span>) ~= idx ? <span class="" style="color: rgb(211, 54, 130);">self</span>[idx] : <span class="" style="color: rgb(211, 54, 130);">nil</span> }</div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(211, 54, 130);">set</span> { <span class="" style="color: rgb(211, 54, 130);">if</span> (<span class="" style="color: rgb(39, 139, 210);">startIndex</span> ..< <span class="" style="color: rgb(39, 139, 210);">endIndex</span>) ~= idx && newValue != <span class="" style="color: rgb(211, 54, 130);">nil</span> { <span class="" style="color: rgb(211, 54, 130);">self</span>[idx] = newValue! } }</div><div class="" style="margin: 0px; line-height: normal;"> }</div><div class="" style="margin: 0px; line-height: normal;">}</div></div></div></div></blockquote></div><div class=""><br class=""></div><div class=""> // array[index1] is only set if both indexes are valid</div><div class=""> array[ifExists: index1] = array[ifExists: index2] </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> 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></div><div>You are right, of course!</div><div><br></div><div>Actually the code as written above already works exactly like that (I just tried on IBM's Swift sandbox): if the second index is out of bounds, the value won't get changed, otherwise it will be changed and the new value will be nil if the array did contain nil at the position of the second index.</div><div><br></div><div>-Thorsten </div><div><br></div><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>