<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 <<a href="mailto:lshsousa@gmail.com" class="">lshsousa@gmail.com</a>> 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 <i class="">swift-ly</i> 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..<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..<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..<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 <i class="">nicer</i> design. But what Haravikk and Chris L. already said seems to me as <i class="">wiser</i> 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 <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 <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=""> array[bounds]</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class=""> 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=""> </span><span style="font-family: Menlo; font-size: 11px;" class="">// </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=""> array.prefix(upTo: index)</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class=""> array.prefix(through: index)</span></div><div class=""><span style="font-size: 11px; font-family: Menlo;" class=""> array.dropLast(n)</span><span style="font-family: Menlo; font-size: 11px;" class=""> </span><span style="font-family: Menlo; font-size: 11px;" class="">// </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=""> array.dropFirst(n)</span></font><span style="font-family: Menlo; font-size: 11px;" class=""> </span><span style="font-family: Menlo; font-size: 11px;" class="">// </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=""> array.suffix(from: index)</span></div><div class=""><div class=""><div class=""><font face="Menlo" class=""><span style="font-size: 11px;" class=""> 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="">) // 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>