<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>I'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'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 >= array.endIndex : sets last element</div><div>// if index < 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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8">Hi there!<div class=""><br class=""></div><div class="">From time to time, I find myself using safe subscripting, e.g.:</div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; 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);" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> array <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> [<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>, <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">2</span>, <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">3</span>]
array[safe: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0</span>] <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// 1</span>
array[safe: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">1</span>] <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// 2</span>
array[safe: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">2</span>] <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// 3</span>
array[safe: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">3</span>] <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// nil</span></span></font></pre><div class=""><br class=""></div><div class="">… with the following implementation:</div></div><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; 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);" class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">subscript</span> (safe index: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> Element? {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">indices</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">~=</span> index <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">?</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">self</span>[index] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">nil</span>
}</span></font></pre><div class=""><br class=""></div></div><div class="">I was wondering … wouldn’t it be handy to have this in the standard library?</div><div class=""><br class=""></div><div class="">Best regards,</div><div class="">Rudolf Adamkovic</div><div class=""><br class=""></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">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>