<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="">Ok, if the subscript label on the second one isn’t the same as the first one, it works. I’m still not sure why what I wrote earlier today was ambiguous, but this seems to work:<div class=""><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(211, 54, 130);" class="">extension<span style="color: rgb(147, 161, 161);" class="">&nbsp;</span><span style="color: rgb(39, 139, 210);" class="">Array</span><span style="color: rgb(147, 161, 161);" class="">&nbsp;{</span></div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">subscript</span>(failableLookup idx:&nbsp;<span style="color: rgb(39, 139, 210);" class="">Index</span>) -&gt;&nbsp;<span style="color: rgb(39, 139, 210);" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">get</span>&nbsp;{&nbsp;<span style="color: rgb(211, 54, 130);" class="">return</span>&nbsp;(<span style="color: rgb(39, 139, 210);" class="">startIndex</span>&nbsp;..&lt;&nbsp;<span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx ?&nbsp;<span style="color: rgb(211, 54, 130);" class="">self</span>[idx] :&nbsp;<span style="color: rgb(211, 54, 130);" class="">nil</span>&nbsp;}</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">set</span>&nbsp;{&nbsp;<span style="color: rgb(211, 54, 130);" class="">if</span>&nbsp;(<span style="color: rgb(39, 139, 210);" class="">startIndex</span>&nbsp;..&lt;&nbsp;<span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx &amp;&amp; newValue !=&nbsp;<span style="color: rgb(211, 54, 130);" class="">nil</span>&nbsp;{&nbsp;<span style="color: rgb(211, 54, 130);" class="">self</span>[idx] = newValue! } }</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class="">}</div><div style="margin: 0px; line-height: normal; font-family: 'Fira Mono'; color: rgb(147, 161, 161);" class=""><div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(211, 54, 130);" class="">extension</span>&nbsp;<span style="color: rgb(39, 139, 210);" class="">Array</span>&nbsp;<span style="color: rgb(211, 54, 130);" class="">where</span>&nbsp;Element: NilLiteralConvertible {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">subscript</span>(nilConvertible idx:&nbsp;<span style="color: rgb(39, 139, 210);" class="">Index</span>) -&gt;&nbsp;<span style="color: rgb(39, 139, 210);" class="">Element</span>? {</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">get</span>&nbsp;{&nbsp;<span style="color: rgb(211, 54, 130);" class="">return</span>&nbsp;(<span style="color: rgb(39, 139, 210);" class="">startIndex</span>&nbsp;..&lt;&nbsp;<span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx ?&nbsp;<span style="color: rgb(211, 54, 130);" class="">self</span>[idx] :&nbsp;<span style="color: rgb(211, 54, 130);" class="">nil</span>&nbsp;}</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: rgb(211, 54, 130);" class="">set</span>&nbsp;{&nbsp;<span style="color: rgb(211, 54, 130);" class="">if</span>&nbsp;(<span style="color: rgb(39, 139, 210);" class="">startIndex</span>&nbsp;..&lt;&nbsp;<span style="color: rgb(39, 139, 210);" class="">endIndex</span>) ~= idx {&nbsp;<span style="color: rgb(211, 54, 130);" class="">self</span>[idx] = newValue ?? Element(nilLiteral: ())} }</div><div style="margin: 0px; line-height: normal;" class="">&nbsp; &nbsp;&nbsp;}</div><div style="margin: 0px; line-height: normal;" class="">}</div></div></div><div class=""><br class=""></div><div class="">Seems kinda “hacky”, though, to need the 2nd set argument labels.</div><div class=""><br class=""></div><div class="">Anyway, I hope this helps.</div><div class=""><br class=""></div><div class="">- Dave Sweeris</div>
<br class=""><div><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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>