<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">I think this maybe a good idea for someone. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">However, you add another burden by dealing with the optional. So the choices facing the programmers are choose from bound checking to optional dealing. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jan 17, 2016 at 9:55 AM, Maximilian Hünenberger <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></div><div>It is true that the setter could cause some confusion in the first place but I think of these particular setters as optional chaining:</div><div><br></div><div>person.residence?.address = &quot;1 Infinite Loop&quot;</div><div><br></div><div>Where the `address` is not set when `residence` is nil. So it&#39;s like the array is nil/not defined if you access it out of bounds.</div><div><br></div><div><br></div><div>In addition subscripts imply O(1) access/set so the expanding array setter should be implemented as method.</div><div><br></div><div>- Maximilian</div><div><div class="h5"><div><br>Am 17.01.2016 um 02:29 schrieb <a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>:<br><br></div><blockquote type="cite"><div>I’m definitely +1 on the getter. I have reservations on the setter. As described, this code:<blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal"><span style="color:#bb2ca2">subscript</span>(safe index: <span style="color:#703daa">Index</span>) -&gt; <span style="color:#703daa">Element</span>? {</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">    <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">self</span>.indices ~= index ? <span style="color:#bb2ca2">self</span>[index] : <span style="color:#bb2ca2">nil</span></div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">}</div></div></div></blockquote><div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal"><span style="color:rgb(187,44,162)">subscript</span>(clamp index: <span style="color:rgb(112,61,170)">Index</span>) -&gt; <span style="color:rgb(112,61,170)">Element</span>? {</div></div></div><div><div style="margin:0px;line-height:normal"><div style="margin:0px;line-height:normal"><font face="Menlo"><span style="font-size:11px">    </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">get</span><font face="Menlo"><span style="font-size:11px"> { </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">return</span><font face="Menlo"><span style="font-size:11px"> </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">self</span><font face="Menlo"><span style="font-size:11px">.indices ~= index ? </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">self</span><font face="Menlo"><span style="font-size:11px">[index] : </span></font><span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">nil</span><font face="Menlo"><span style="font-size:11px"> } </span></font><font color="#008400" face="Menlo" style="font-family:Menlo;font-size:12px"><span style="font-size:11px">// again, because subscripts can</span></font><font color="#008400" face="Menlo"><span style="font-size:11px">’t be setter-only</span></font></div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">    <span style="color:rgb(187,44,162)">set</span> {</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">        <span style="color:rgb(187,44,162)">if</span> <span style="color:rgb(187,44,162)">self</span>.indices ~= index {</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">            <span style="color:rgb(187,44,162)">self</span>[index] = newValue!</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">        } <span style="color:rgb(187,44,162)">else</span> {</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">            <span style="color:rgb(187,44,162)">self</span>.append(newValue!)</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">        }</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">    }</div></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><div style="margin:0px;line-height:normal">}</div><div><br></div></div></div></blockquote></div><div>Would lead to some counter-intuitive results.</div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span> foo = [<span style="color:#272ad8">0</span>, <span style="color:#272ad8">1</span>, <span style="color:#272ad8">2</span>]</div></div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px;color:rgb(61,29,129)">print(<span style="color:rgb(79,129,135)">foo</span><span style="color:rgb(49,89,93)">[</span>safe: </span><font color="#272ad8" style="font-family:Menlo;font-size:11px">2</font><span style="font-family:Menlo;font-size:11px;color:rgb(49,89,93)">]</span><font color="#3d1d81" style="font-family:Menlo;font-size:11px">) </font><font color="#008400" face="Menlo"><span style="font-size:11px">// Optional(2)… Yep</span></font></div></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#4f8187">foo</span><span style="color:#31595d">[</span>clamp: <span style="color:#272ad8">6</span><span style="color:#31595d">]</span> = <span style="color:#272ad8">6</span></div></div><div><div style="margin:0px;line-height:normal"><font color="#3d1d81" face="Menlo"><span style="font-size:11px">print(</span></font><span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px">foo</span><span style="color:rgb(49,89,93);font-family:Menlo;font-size:11px">[</span><font color="#3d1d81" face="Menlo"><span style="font-size:11px">safe: </span></font><span style="color:rgb(39,42,216);font-family:Menlo;font-size:11px">6</span><span style="color:rgb(49,89,93);font-family:Menlo;font-size:11px">]</span><font color="#3d1d81" face="Menlo"><span style="font-size:11px">)</span></font><font color="#3d1d81" style="color:rgb(61,29,129);font-family:Menlo;font-size:11px"> </font><font color="#008400" face="Menlo"><span style="font-size:11px">// nil… </span></font><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">Wait, what? I just set that to 6! Why is it nil now?</span></div></div></blockquote><div><div><br></div><div><br></div></div><div><div>Instead of just appending the new value, we could extend the array to the required capacity:</div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">subscript</span>(safe index: <span style="color:#703daa">Index</span>) -&gt; <span style="color:#703daa">Element</span>? {</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">get</span> { <span style="color:#bb2ca2">return</span> <span style="color:#bb2ca2">self</span>.indices ~= index ? <span style="color:#bb2ca2">self</span>[index] : <span style="color:#bb2ca2">nil</span> }</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:#bb2ca2">set</span> {</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">self</span>.indices ~= index {</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:#bb2ca2">self</span>[index] = newValue!</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        } <span style="color:#bb2ca2">else</span> {</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            <span style="color:#bb2ca2">while</span> !(<span style="color:#bb2ca2">self</span>.indices ~= index) {</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">                <span style="color:#bb2ca2">self</span>.append(newValue!)</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">            }</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        }</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div></div></div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div></div></blockquote><div><div><br></div><div>The results could still be unexpected:</div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">var</span> foo = [<span style="color:#272ad8">0</span>, <span style="color:#272ad8">1</span>, <span style="color:#272ad8">2</span>]</div></div></div><div><div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(61,29,129)">print<span style="color:rgb(0,0,0)">(</span><span style="color:rgb(79,129,135)">foo</span><span style="color:rgb(0,0,0)">) <span style="white-space:pre-wrap">                </span></span><span style="color:rgb(0,132,0)">// [0, 1, 2]… Yep</span></div></div><div><div></div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(61,29,129)"><span style="color:rgb(79,129,135)">foo</span><span style="color:rgb(49,89,93)">[</span>safe: </span><font color="#272ad8">9</font><span style="color:rgb(49,89,93)">]</span><font color="#3d1d81"> = </font><font color="#272ad8">9</font></div></div></div><div><div><div style="margin:0px;line-height:normal"><span style="font-family:Menlo;font-size:11px;color:rgb(61,29,129)">print</span><span style="font-family:Menlo;font-size:11px">(</span><span style="font-family:Menlo;font-size:11px;color:rgb(79,129,135)">foo</span><span style="font-family:Menlo;font-size:11px;color:rgb(49,89,93)">[</span><span style="font-family:Menlo;font-size:11px">safe: <font color="#272ad8">9</font></span><span style="font-family:Menlo;font-size:11px;color:rgb(49,89,93)">]</span><span style="font-family:Menlo;font-size:11px">)</span><font color="#3d1d81" style="font-family:Menlo;font-size:11px"> <span style="white-space:pre-wrap">        </span></font><font color="#008400" face="Menlo" style="font-family:Menlo;font-size:11px">// Optional(9)… Yep</font></div></div></div></blockquote><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><div><div style="margin:0px;line-height:normal"><font color="#3d1d81" face="Menlo"><span style="font-size:11px">print</span></font><span style="font-family:Menlo;font-size:11px">(</span><span style="color:rgb(79,129,135);font-family:Menlo;font-size:11px">foo</span><span style="font-family:Menlo;font-size:11px">)</span><font color="#3d1d81" face="Menlo"><span style="font-size:11px"> <span style="white-space:pre-wrap">                </span></span></font><span style="color:rgb(0,132,0);font-family:Menlo;font-size:11px">// [0, 1, 2, 9, 9, 9, 9, 9, 9]… Wow, that</span><font color="#008400" face="Menlo"><span style="font-size:11px">’s bigger than I thought</span></font></div></div></div></div></blockquote><div><div><div></div></div></div><div><div><br></div><div>but at least you’d be able to read from the index to which you just wrote.</div><br><div>
- Dave Sweeris

</div>
<br><div><blockquote type="cite"><div>On Jan 16, 2016, at 14:08, Maximilian Hünenberger via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div dir="auto"><div></div><div>I&#39;ve also thought of this. But the concept of safe indexing can also be extended to CollectionType (or to the more abstract Indexable protocol) where it&#39;s index is Comparable.</div><div><br></div><div>For mutable Collections/MutableIndexable there could be two versions of a safe subscript set:</div><div><br></div><div>array[safe: 5] = 7 // does nothing if index out of bounds</div><div><br></div><div>array[clamp: 5] = 7</div><div>// if array is empty: does nothing</div><div>// if 5 &gt;= array.endIndex : sets last element</div><div>// if index &lt; 0 : sets first index</div><div><br></div><div>So definitely worth considering.</div><div><br></div><div><br></div><div>- Maximilian</div><div><br>Am 16.01.2016 um 21:44 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>Hi there!<div><br></div><div>From time to time, I find myself using safe subscripting, e.g.:</div><div><br></div><div><pre style="overflow:auto;margin-top:0px;margin-bottom:0px;line-height:1.45;padding:16px;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-wrap:normal;word-break:normal;color:rgb(51,51,51)"><font face="Menlo"><span style="font-size:11px"><span style="color:rgb(167,29,93)">let</span> array <span style="color:rgb(167,29,93)">=</span> [<span style="color:rgb(0,134,179)">1</span>, <span style="color:rgb(0,134,179)">2</span>, <span style="color:rgb(0,134,179)">3</span>]

array[safe: <span style="color:rgb(0,134,179)">0</span>] <span style="color:rgb(150,152,150)">// 1</span>
array[safe: <span style="color:rgb(0,134,179)">1</span>] <span style="color:rgb(150,152,150)">// 2</span>
array[safe: <span style="color:rgb(0,134,179)">2</span>] <span style="color:rgb(150,152,150)">// 3</span>
array[safe: <span style="color:rgb(0,134,179)">3</span>] <span style="color:rgb(150,152,150)">// nil</span></span></font></pre><div><br></div><div>… with the following implementation:</div></div><div><br></div><div><pre style="overflow:auto;margin-top:0px;margin-bottom:0px;line-height:1.45;padding:16px;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-wrap:normal;word-break:normal;color:rgb(51,51,51)"><font face="Menlo"><span style="font-size:11px"><span style="color:rgb(167,29,93)">subscript</span> (safe index: <span style="color:rgb(0,134,179)">Int</span>) <span style="color:rgb(167,29,93)">-&gt;</span> Element? {
    <span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(167,29,93)">self</span><span style="color:rgb(167,29,93)">.</span><span style="color:rgb(0,134,179)">indices</span> <span style="color:rgb(167,29,93)">~=</span> index <span style="color:rgb(167,29,93)">?</span> <span style="color:rgb(167,29,93)">self</span>[index] <span style="color:rgb(167,29,93)">:</span> <span style="color:rgb(0,134,179)">nil</span>
}</span></font></pre><div><br></div></div><div>I was wondering … wouldn’t it be handy to have this in the standard library?</div><div><br></div><div>Best regards,</div><div>Rudolf Adamkovic</div><div><br></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><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></div></blockquote></div>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></blockquote></div></div></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>