<div dir="ltr">This proposal seeks to provide a safer <font face="monospace, monospace">..<</font> (aka half-open range operator) in order to avoid **Array index out of range** errors in execution time.<br><br>Here is my first draft for this proposal: <a href="https://github.com/luish/swift-evolution/blob/half-open-range-operator/proposals/nnnn-safer-half-open-range-operator.md">https://github.com/luish/swift-evolution/blob/half-open-range-operator/proposals/nnnn-safer-half-open-range-operator.md</a><br><br>In short, doing that in Swift causes a runtime error:<div>
<p class=""><font face="monospace, monospace"><span class="">let</span><span class=""> a </span><span class="">=</span><span class=""> [</span><span class="">1</span><span class="">,</span><span class="">2</span><span class="">,</span><span class="">3</span><span class="">]<br></span><span class="">let</span><span class=""> b </span><span class="">=</span><span class=""> a[</span><span class="">0</span><span class="">..<</span><span class="">5</span><span class="">]<br></span><span class="">print</span><span class="">(b)</span></font></p>
<p class=""><span class="">> Error running code: <br></span>> fatal error: Array index out of range</p>
<p class=""><span class="">The proposed solution is to slice the array retur</span>ning all elements that are below the half-open operator, even though the number of elements is lesser than the ending of the half-open operator. So the example above would return [1,2,3]. <br>We can see this very behaviour in other languages, such as Python and Ruby as shown in the proposal draft.</p><p class="">This would eliminate the need for verifications on the array size before slicing it -- and consequently runtime errors in cases when the programmer didn't. <br><br>Viewing that it is my very first proposal, any feedback will be helpful.</p></div><div><div class="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Thanks!</div><div></div><br></div><div>Luis Henrique Borges</div><div>@luishborges</div></div></div></div></div></div></div></div></div>
</div>