<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="">In principle, I’m not opposed to that. In practice, though… unless you’re also proposing we give it “magic” syntax like what Optionals currently have, we’d have to manually wrap/unwrap the values:<div class=""><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">bar[<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">ifExists: </span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span>]<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> = </span>Result<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">.</span>result<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">4</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class="">)</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(203, 75, 22);" class=""><span style="color: rgb(39, 139, 210);" class="">print</span><span style="color: rgb(147, 161, 161);" class="">(</span><span style="color: rgb(41, 161, 152);" class="">bar[</span><span style="color: rgb(147, 161, 161);" class="">ifExists: </span><span style="color: rgb(108, 113, 196);" class="">4</span><span style="color: rgb(41, 161, 152);" class="">]</span><span style="color: rgb(147, 161, 161);" class="">.</span><span style="color: rgb(41, 161, 152);" class="">result</span><span style="color: rgb(147, 161, 161);" class="">)</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: #cb4b16" class=""><br class=""></span></div><div class="">Which would strictly speaking work, but IMHO is more annoying than just manually checking <span style="font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">collection</span><span style="font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">.</span><span style="color: rgb(39, 139, 210); font-family: 'Fira Mono';" class="">indices</span><span style="font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">.</span><span style="color: rgb(39, 139, 210); font-family: 'Fira Mono';" class="">contains</span><span style="font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">(</span><span style="font-family: 'Fira Mono'; color: rgb(41, 161, 152);" class="">idx</span><span style="font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">)</span> before attempting the subscript.</div><div class=""><br class=""></div><div class=""><div class="">
- Dave Sweeris
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">On Feb 6, 2016, at 13:21, Thorsten Seitz 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 dir="auto" class=""><div class=""><span class=""></span></div><div class=""><div class=""><span class=""></span></div><div class=""><div class=""></div><div class=""><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">Ah ok, I was using the other version which distinguishes between nil and Optional(nil):</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div 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;"><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">extension<span class=""> </span><span class="">Array</span><span class=""> {</span></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">subscript</span>(ifExists idx: <span class="">Index</span>) -> <span class="">Element</span>? {</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> <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></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> <span class="">set</span> { <span class="">if</span> (<span class="">startIndex</span> ..< <span class="">endIndex</span>) ~= idx && newValue != <span class="">nil</span> { <span class="">self</span>[idx] = newValue! } }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""> }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">}</span></font></div></div></div></div></div></div></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">Where you can assign Optional(nil) to set a value to nil which is admittedly not very intuitive...</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">array[ifExists: 0] = Optional(nil)</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">print(array) // "[nil]"</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">array = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">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);" class="">print(array) // "[Optional(1)]"</span></div></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">A solution would probably be to introduce another enum mimicking Optional but built for the array subscript which allows to distinguish both cases.</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">-Thorsten </span></div><div class=""><br class="">Am 06.02.2016 um 21:52 schrieb Maximilian Hünenberger <<a href="mailto:m.huenenberger@me.com" class="">m.huenenberger@me.com</a>>:<br class=""><br class=""></div><blockquote type="cite" class=""><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">var array: [Int?] = [1]</span></div><div class="" style="margin: 0px; line-height: normal;"><span style="background-color: rgba(255, 255, 255, 0);" class="">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);" class="">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);" class="">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);" class="">print(array) // "[nil]"</span></div></blockquote></div></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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></div></body></html>