<div dir="ltr">Here&#39;s real working code you can tinker with in Swift 2.1:<div><br></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">extension<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(112,61,170)">Indexable</span><span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">subscript</span>(ifExists index: <span style="color:rgb(112,61,170)">Index</span>) -&gt; <span style="color:rgb(112,61,170)">_Element</span>? {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">get</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">if</span> <span style="color:rgb(112,61,170)">startIndex</span>.<span style="color:rgb(61,29,129)">distanceTo</span>(index) <span style="color:rgb(61,29,129)">&lt;</span> <span style="color:rgb(112,61,170)">startIndex</span>.<span style="color:rgb(61,29,129)">distanceTo</span>(<span style="color:rgb(112,61,170)">endIndex</span>) {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">self</span>[index]</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">nil</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p></div><div>You can make the setter work and factor out some of the ugly if you wanted. I didn&#39;t want to. I have no desire to see this in the language. I just wanted to point out that developers who want it can have it today.</div><div><br></div><div>-david  <a href="https://github.com/AE9RB/SwiftGL" target="_blank">https://github.com/AE9RB/SwiftGL</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 31, 2016 at 3:21 PM, David Sweeris via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>My recollection from the discussion thread is that we didn&#39;t really reach a firm consensus on the setter semantics, mostly because Swift doesn&#39;t support setter-only subscripts, and we couldn&#39;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><br></div><div>Don&#39;t misunderstand me... I&#39;m definitely +1 on this, I&#39;m just not a fan of &quot;ifExists&quot; (no, I don&#39;t have any better ideas)</div><div><br>- Dave Sweeris</div><div><div class="h5"><div><br>On Jan 31, 2016, at 15:07, Maximilian Hünenberger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div></div><div>The setter of the subscript should be:</div><div><div><div dir="auto"><div><div><div><div style="word-wrap:break-word"><div><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)"><span><br></span></span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)"><span>set</span> {</span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">    <span>if</span> <span>self</span>.indices ~= index &amp;&amp; newValue != <span>nil</span> {</span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">        <span>self</span>[index] = newValue!</span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">    }</span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">}</span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)"><br></span></font></div><div style="margin:0px;line-height:normal"><font color="#000000"><span style="background-color:rgba(255,255,255,0)">Since &quot;newValue&quot; is of type &quot;Element?&quot;.</span></font></div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">It seems that this subscript could also be added to &quot;CollectionType&quot; and &quot;MutableCollectionType&quot;.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">The setter is weird because you can use an optional element:</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">var arr = [1]</div><div style="margin:0px;line-height:normal">// is valid but doesn&#39;t set the first element</div><div style="margin:0px;line-height:normal">arr[ifExists: 0] = nil</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">var arr2: [Int?] = [1]</div><div style="margin:0px;line-height:normal">arr2[ifExists: 0] = nil // changes nothing</div><div style="margin:0px;line-height:normal">arr2[ifExists: 0] = .Some(nil) // sets first element to nil : arr2 == [nil]</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">I don&#39;t know whether a setter should be added at all.</div><div style="margin:0px;line-height:normal"><br></div><div 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" target="_blank">swift-evolution@swift.org</a>&gt;:<br><br></div><blockquote type="cite"><div><div>All right, I put together a proposal:</div><div><br></div><div><a href="https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md" target="_blank">https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md</a></div><div><br></div><div>… and opened a PR:</div><div><br></div><div><a href="https://github.com/apple/swift-evolution/pull/133" target="_blank">https://github.com/apple/swift-evolution/pull/133</a></div><div><br></div><div>Let’s see how this goes.</div><div><br></div><div>R+</div></div></blockquote></div></blockquote></div></div><blockquote type="cite"><div><span>_______________________________________________</span><span class=""><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></span></div></blockquote></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>