<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>My recollection from the discussion thread is that we didn't really reach a firm consensus on the setter semantics, mostly because Swift doesn't support setter-only subscripts, and we couldn't come up with one argument label that both read well as both a setter &amp; a getter, *and* clearly meant that the assignment itself could fail.</div><div id="AppleMailSignature"><br></div><div id="AppleMailSignature">Don't misunderstand me... I'm definitely +1 on this, I'm just not a fan of "ifExists" (no, I don't have any better ideas)</div><div id="AppleMailSignature"><br>- Dave Sweeris</div><div><br>On Jan 31, 2016, at 15:07, Maximilian Hünenberger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><div></div><div>The setter of the subscript should be:</div><div><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 color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"><span class=""><br></span></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"><span class="">set</span>&nbsp;{</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&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 color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span class="">self</span>[index] = newValue!</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; }</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">}</span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);"><br></span></font></div><div class="" style="margin: 0px; line-height: normal;"><font color="#000000"><span style="background-color: rgba(255, 255, 255, 0);">Since "newValue" is of type "Element?".</span></font></div><div class="" style="margin: 0px; line-height: normal;"><br></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></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></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></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></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></div><div class="" style="margin: 0px; line-height: normal;">- Maximilian</div></div></div></blockquote></div></div></div></div></div></div></div></div></div><div><br>Am 31.01.2016 um 23:38 schrieb Rudolf Adamkovič 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="">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></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></body></html>