<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&nbsp;<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>&nbsp;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 &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=""><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="">&nbsp;</span><span class="">Array</span><span class="">&nbsp;{</span></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&nbsp; &nbsp;&nbsp;<span class="">subscript</span>(ifExists idx:&nbsp;<span class="">Index</span>) -&gt;&nbsp;<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="">&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></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">&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 &amp;&amp; newValue !=&nbsp;<span class="">nil</span>&nbsp;{&nbsp;<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="">&nbsp; &nbsp; }</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] =&nbsp;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&nbsp;</span></div><div class=""><br class="">Am 06.02.2016 um 21:52 schrieb Maximilian Hünenberger &lt;<a href="mailto:m.huenenberger@me.com" class="">m.huenenberger@me.com</a>&gt;:<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] =&nbsp;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>