<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 18, 2015, at 1:46 PM, Joe Groff via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: AvenirNext-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Dec 18, 2015, at 4:42 AM, Amir Michail via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Examples:<div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l=[1,2,3,4,5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[-1]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">5</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[-2]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">4</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[2:4]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[3, 4]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[2:]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[3, 4, 5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[-2:]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[4, 5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[:3]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[1, 2, 3]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[::2]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[1, 3, 5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">>>> l[::]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[1, 2, 3, 4, 5]</div></div></div></div></blockquote><br class=""></div><div style="font-family: AvenirNext-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Accepting negative indices is problematic for two reasons: it imposes runtime overhead in the index operation to check the sign of the index; also, it masks fencepost errors, since if you do foo[m-n] and n is accidentally greater than m, you'll quietly load the wrong element instead of trapping. I'd prefer something like D's `$-n` syntax for explicitly annotating end-relative indexes.</div></div></blockquote><br class=""></div><div>Yes, we already have facilities to do most of what Python can do here, but one major problem IMO is that the “language” of slicing is so non-uniform: we have [a..<b], dropFirst, dropLast, prefix, and suffix. Introducing “$” for this purpose could make it all hang together<span style="font-family: AvenirNext-Regular;" class=""> and also eliminate the “why does it have to be so hard to look at the 2nd character of a string?!” problem. That is, use the identifier “$” (yes, that’s an identifier in Swift) to denote the beginning-or-end of a collection. Thus,</span></div><div><div style="font-family: AvenirNext-Regular;" class=""><br class=""></div><div style="font-family: AvenirNext-Regular;" class=""><font face="Menlo" class=""> c[c.startIndex.advancedBy(3)] =><span class="Apple-tab-span" style="white-space: pre;">        </span>c[$+3] // Python: c[3]</font></div><div style="font-family: AvenirNext-Regular;" class=""><div class=""><font face="Menlo" class=""> c[c.endIndex.advancedBy(-3)] =><span class="Apple-tab-span" style="white-space: pre;">        </span>c[$-3] // Python: c[-3]</font></div><div class=""></div><div class=""><span class="" style="font-family: Menlo;"> c.dropFirst(3) =></span><span class="Apple-tab-span" style="font-family: Menlo; white-space: pre;">                        </span><span class="" style="font-family: Menlo;">c[$+3...] // Python: c[3:]</span></div></div><div style="font-family: AvenirNext-Regular;" class=""><font face="Menlo" class=""> c.dropLast(3) =><span class="Apple-tab-span" style="white-space: pre;">                        </span>c[..<$-3] // Python: c[:-3]</font></div><div style="font-family: AvenirNext-Regular;" class=""><font face="Menlo" class=""> c.prefix(3) =><span class="Apple-tab-span" style="white-space: pre;">                        </span>c[..<$+3] // Python: c[:3]</font></div><div style="font-family: AvenirNext-Regular;" class=""><font face="Menlo" class=""> c.suffix(3) => <span class="Apple-tab-span" style="white-space: pre;">                        </span>c[$-3...] // Python: c[-3:]</font></div><div style="font-family: AvenirNext-Regular;" class=""> </div></div><div class=""><span style="font-family: AvenirNext-Regular;" class="">It even has the nice connotation that, “this might be a little more expen</span><font face="Menlo" class="">$</font><span style="font-family: AvenirNext-Regular;" class="">ive than plain indexing” (which it might, for non-random-access collections). </span>I think the syntax is still a bit heavy, not least because of “..<“ and “...”, but the direction has potential. </div><div class=""><br class=""></div><div class=""> I haven’t had the time to really experiment with a design like this; the community might be able to help by prototyping and using some alternatives. You can do all of this outside the standard library with extensions.</div><br class=""><div class="">
-Dave<div class=""><br class=""></div><br class="Apple-interchange-newline">
</div>
<br class=""></body></html>