<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> <span class="">Array</span> <span class="">where</span> Element: NilLiteralConvertible {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"> <span class="">subscript</span>(ifExists idx: <span class="">Index</span>) -> <span class="">Element</span>? {</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"> <span class="">get</span> { <span class="">return</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx ? <span class="">self</span>[idx] : <span class="">nil</span> }</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);"> <span class="">set</span> { <span class="">if</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx { <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);"> }</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] = 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] = 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 <<a href="mailto:tseitz42@icloud.com">tseitz42@icloud.com</a>>:<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 <<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"><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> var array: [Int?] = [1]</div><div> 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> <span style="background-color: rgba(255, 255, 255, 0);">array[ifExists: 0] = array[ifExists: 1]</span></div></div></blockquote><div><br></div>No, it doesn't. Just try it out.<div><br></div><div>-Thorsten </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 <<a href="mailto:swift-evolution@haravikk.me">swift-evolution@haravikk.me</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></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>