<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><blockquote type="cite" class=""><div class="">On 13 Apr 2016, at 17:53, Luis Henrique B. Sousa &lt;<a href="mailto:lshsousa@gmail.com" class="">lshsousa@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">(…) I totally agree with @Vladimir that we could have a more clear and&nbsp;<i class="">swift-ly</i>&nbsp;way to concisely wrap those operations.<div class=""><div class="gmail_extra"><div class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class="" style="font-size: small;"><br class=""></div><div class="" style="font-size: small;">The behaviour pointed out by him l<span class="" style="font-size: 12.8px;">ooks very nice and doable to me.</span></div><div class="" style="font-size: small;"><br class=""></div><div dir="ltr" class="" style="font-size: small;"><font face="monospace, monospace" class=""><span class="" style="font-size: 12.8px;">a = [1,2,3]</span><br class="" style="font-size: 12.8px;"><span class="" style="font-size: 12.8px;">a[-1..&lt;6] - raises runtime error (r</span><span class="" style="font-size: 12.8px;">ight behavior by default, doesn't affect existing code)</span><br class="" style="font-size: 12.8px;"><span class="" style="font-size: 12.8px;">a[truncate: -1..&lt;6] - produces [1,2,3] (the very behaviour I proposed initially)</span><br class="" style="font-size: 12.8px;"><span class="" style="font-size: 12.8px;">a[safe: -1..&lt;6] - produces nil (i.e [T]?) (no runtime errors and makes it easy to handle unexpected results)</span></font></div></div></div></div></div></div></div></div></div></div></div></div></div></div></blockquote></div><div class=""><div class=""><div dir="ltr" class=""><div class=""><div class="gmail_extra"><div class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class="" style="font-size: small;"><font face="monospace, monospace" class=""><span class="" style="font-size: 12.8px;"><br class=""></span></font></div></div></div></div></div></div></div></div></div></div></div></div></div></div></div><div class="">I don't feel strongly about this. Yes, if this were shorter to express, it would feel like&nbsp;<i class="">nicer</i>&nbsp;design. But what Haravikk and Chris L. already said seems to me as&nbsp;<i class="">wiser</i>&nbsp;design.</div><div class=""><br class=""></div><div class="">(@Vladimir: Besides, I'm sure `<font face="Menlo" class=""><span style="font-size: 11px;" class="">.clamped(to:)</span></font>` wasn't invented for this purpose but for doing interval arithmetic on ranges. It just happens to somewhat work here.)</div><div class=""><br class=""></div><div class="">– Would this feature really provide a measurable benefit to developers?</div><div class="">– Under which circumstances do you find yourself with a past-the-end upper bound such as&nbsp;<font face="Menlo" class=""><span style="font-size: 11px;" class="">6</span></font> where `<font face="Menlo" class=""><span style="font-size: 11px;" class="">a.count == 3</span></font>`?</div><div class="">– …Let alone a <i class="">negative</i> start index like `<font face="Menlo" class=""><span style="font-size: 11px;" class="">-1</span></font>`?</div><div class=""><br class=""></div><div class="">I find cases like these to be much more common in languages like Python and Ruby where e.g. `<font face="Menlo" class=""><span style="font-size: 11px;" class="">array[-2]</span></font>` refers to the penultimate element. Swift doesn't seem to want to go there.</div><div class=""><br class=""></div><div class="">For the use cases I can think of, Swift 3—as the&nbsp;<a href="https://github.com/apple/swift/blob/swift-3-indexing-model/stdlib/public/core/Collection.swift#L724-L808" class="">proposal currently goes</a>—already offers the following suitable methods:</div><div class=""><br class=""></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; array[bounds]</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; array.prefix(</span><span style="font-family: Menlo; font-size: 11px;" class="">maxLength</span><span style="font-size: 11px; font-family: Menlo;" class="">)</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">//&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">no precondition</span></div><div class=""><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; array.prefix(upTo: index)</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; array.prefix(through: index)</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; array.dropLast(n)</span><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">//&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">no precondition</span></div><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; array.dropFirst(n)</span></font><span style="font-family: Menlo; font-size: 11px;" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">//&nbsp;</span><span style="font-family: Menlo; font-size: 11px;" class="">no precondition</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class="">&nbsp; &nbsp; array.suffix(from: index)</span></div><div class=""><div class=""><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class="">&nbsp; &nbsp; array.suffix(</span></font><span style="font-family: Menlo; font-size: 11px;" class="">maxLength</span><font face="Menlo" class=""><span style="font-size: 11px;" class="">) &nbsp; &nbsp; &nbsp;// no precondition</span></font></div></div></div><div class=""><br class=""></div><div class="">If these feel too clumsy to use, maybe we should focus on making them all more convenient. Ideally, that suggestion would apply to all `<font face="Menlo" class=""><span style="font-size: 11px;" class="">Collection</span></font>`s.</div><br class="">— Pyry<div class=""><br class=""></div></body></html>