<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div><br></div><div>Am 04.02.2016 um 21:24 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 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></blockquote><div><br></div>Why is that unfortunate? Isn't it exactly what you specified in your assignment?<div><br></div><div>-Thorsten&nbsp;</div><div><br></div><div><br><blockquote type="cite"><div><div class="">- Maximilian</div><br class=""><div><blockquote type="cite" class=""><div class="">Am 01.02.2016 um 22:34 schrieb <a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>:</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="">Really, it should allow the&nbsp;<span style="color: rgb(211, 54, 130); font-family: 'Fira Mono';" class="">nil</span>&nbsp;to go through for any type that conforms to&nbsp;<span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">NilLiteralConvertible</span>, not just Optionals. This code compiles on its own, but fails when you try to use it with an optional type:</div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><div style="margin: 0px; line-height: normal; color: rgb(211, 54, 130);" class="">extension<span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Array</span><span style="font-variant-ligatures: no-common-ligatures; color: #93a1a1" class=""> {</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span>(failableLookup idx: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Index</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">startIndex</span> ..&lt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">endIndex</span>) ~= idx ? <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] : <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span> }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">set</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">startIndex</span> ..&lt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">endIndex</span>) ~= idx &amp;&amp; newValue != <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = newValue! } }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal;" class="">}</div><div style="margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">extension</span> <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Array</span> <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">where</span> Element: NilLiteralConvertible {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">subscript</span>(failableLookup idx: <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Index</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">get</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">return</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">startIndex</span> ..&lt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">endIndex</span>) ~= idx ? <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] : <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span> }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">set</span> {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">if</span> (<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">startIndex</span> ..&lt; <span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">endIndex</span>) ~= idx {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">switch</span> newValue {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> .None: &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">nil</span></div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">case</span> .Some(<span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">let</span> nv): <span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">self</span>[idx] = nv</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; line-height: normal;" class="">}</div></div><div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #d33682" class="">var</span> optarr: [<span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">Int</span>?] = [<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">1</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">2</span>,<span style="font-variant-ligatures: no-common-ligatures; color: #6c71c4" class="">3</span>]</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #278bd2" class="">print</span>(optarr)</div><div style="margin: 0px; line-height: normal;" class=""><font color="#93a1a1" face="Fira Mono" class="">optarr[failableLookup: </font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">2</span><font color="#93a1a1" face="Fira Mono" class="">] = </font><span style="color: rgb(211, 54, 130); font-family: 'Fira Mono';" class="">nil&nbsp;</span><span style="color: rgb(203, 75, 22); font-family: 'Fira Mono';" class="">//</span><font color="#cb4b16" face="Fira Mono" class="">&nbsp;Ambiguous use of 'subscript(failableLookup:)'</font></div></div><div class=""><br class=""></div><div class="">I don’t understand why the compiler thinks it’s ambiguous, though.&nbsp;<span style="font-family: 'Fira Mono'; color: rgb(39, 139, 210);" class="">Int</span><span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">?</span>&nbsp;is&nbsp;<span style="color: rgb(147, 161, 161); font-family: 'Fira Mono';" class="">NilLiteralConvertible</span>, so the compiler should send it to the more specialized subscript. At least I thought that was how it was supposed to work. It might be a compiler bug, though, because it’s quite confused… It claims its two choices are:</div><div class="">EquationTests.swift:29:9: Found this candidate</div><div class="">EquationTests.swift:29:9: Found this candidate</div><div class=""><br class=""></div><div class="">and that’s the same line that tripped the error in the first place:&nbsp;<font color="#93a1a1" face="Fira Mono" class="">optarr[failableLookup:&nbsp;</font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">2</span><font color="#93a1a1" face="Fira Mono" class="">] =&nbsp;</font><span style="color: rgb(211, 54, 130); font-family: 'Fira Mono';" class="">nil</span></div><div class=""><div class=""><br class=""></div><div class="">Astute readers will notice two things: First, the two candidates are identical, so there’s actually only one candidate. Second,&nbsp;<font color="#93a1a1" face="Fira Mono" class="">optarr[failableLookup:&nbsp;</font><span style="color: rgb(108, 113, 196); font-family: 'Fira Mono';" class="">2</span><font color="#93a1a1" face="Fira Mono" class="">] =&nbsp;</font><span style="color: rgb(211, 54, 130); font-family: 'Fira Mono';" class="">nil&nbsp;</span>does not define a subscript function, ambiguous or otherwise.</div></div><div class=""><br class=""></div><div class="">Anyway, that’s as far as I got with it.</div><div class=""><br class=""></div><div class="">
- Dave Sweeris

</div>
<br class=""><div class=""><blockquote type="cite" class=""><div class="">On Feb 1, 2016, at 00:53, Rudolf Adamkovič 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="">Hi Maximilian,</div><div class=""><br class=""></div><div class="">ah, I see. This is a show stopper then!</div><div class=""><br class=""></div><div class="">From what I imagine, this should not type-check:</div><div class=""><br class=""></div><div class="">var&nbsp;array&nbsp;= [1]</div><div class="">array[ifExists: 0] = nil</div><div class=""><br class=""></div><div class="">… and this should set&nbsp;array[0] to nil:</div><div class=""><br class=""></div><div class=""><div dir="auto" class=""><div class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><div class=""><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; line-height: normal;">var&nbsp;array: [Int?] = [1]</div><div class="" style="margin: 0px; line-height: normal;">array[ifExists: 0] = nil</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">Is it not possible to implement such setter in Swift?</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">R+</div></div></div></div></div></div></div></div></div></div></div></div></div></div><br class=""><div class=""><blockquote type="cite" class=""><div class="">On 1 Feb 2016, at 00:07, Maximilian Hünenberger &lt;<a href="mailto:m.huenenberger@me.com" class="">m.huenenberger@me.com</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=""></div><div class="">The setter of the subscript should be:</div><div class=""><div class=""><div dir="auto" class=""><div class=""><div class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class=""><div class=""><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><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=""><span class=""><br 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="">set</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;<span class="">if</span>&nbsp;<span class="">self</span>.indices ~= index &amp;&amp; newValue !=&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="">self</span>[index] = 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 class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Since "newValue" is of type "Element?".</span></font></div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">It seems that this subscript could also be added to "CollectionType" and "MutableCollectionType".</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">The setter is weird because you can use an optional element:</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">var arr = [1]</div><div class="" style="margin: 0px; line-height: normal;">// is valid but doesn't set the first element</div><div class="" style="margin: 0px; line-height: normal;">arr[ifExists: 0] = nil</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">var arr2: [Int?] = [1]</div><div class="" style="margin: 0px; line-height: normal;">arr2[ifExists: 0] = nil // changes nothing</div><div class="" style="margin: 0px; line-height: normal;">arr2[ifExists: 0] = .Some(nil) // sets first element to nil : arr2 == [nil]</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">I don't know whether a setter should be added at all.</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">- Maximilian</div></div></div></blockquote></div></div></div></div></div></div></div></div></div><div class=""><br class="">Am 31.01.2016 um 23:38 schrieb Rudolf Adamkovič via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div class="">All right, I put together a proposal:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md" class="">https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md</a></div><div class=""><br class=""></div><div class="">… and opened a PR:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/apple/swift-evolution/pull/133" class="">https://github.com/apple/swift-evolution/pull/133</a></div><div class=""><br class=""></div><div class="">Let’s see how this goes.</div><div class=""><br class=""></div><div class="">R+</div></div></blockquote></div></div></blockquote></div><br class=""></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></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></body></html>