<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="">I’ll answer my own question (fired up xcode beta to try some stuff in swift 4):<div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">&nbsp;Is there a way of writing something looks like</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>getLongergDeviceId()[&lt;-2 from end&gt; ... ]</div><div class=""><br class=""></div><div class="">where whatever goes in the &lt;-2 from the end&gt; does NOT refer to getLongerDeviceID()? &nbsp;</div></div></blockquote><div class=""><br class=""></div>and in swift 4, the answer is “yes.” &nbsp;If I simply overload the string subscript operator to operate on CountablePartialRange&lt;Int&gt;, and I assume that positive means offset from the beginning, and negative means relative to the end, then I believe I can write something that is as efficient as one can get, using this syntax:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let shortDeviceId = getLongerDeviceID[(-2)...]</div><div class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;let someOtherSlice = getLongerDeviceID[3...]        // start at 3 from beginning and go to end<br class=""><br class="">So *I’ll* be happy. &nbsp;I just need to overload to handle all the different range types. &nbsp;I’m sure there’s a reason for not including this in the API (i guess there’s a need to be precise about what “units” one is talking about) but for my purposes it gives an efficient and easy to read API.</div><div class=""><br class=""></div><div class="">I could imagine, just to avoid amiguity, perhaps</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>&nbsp;let someOtherSlice = getLongerDeviceID[UTF8.Index(3)...]        // start at 3 from beginning and go to end</div><div class=""><br class=""></div><div class="">which wouldn’t be the end of the world (compared with what we have now).</div><div class=""><br class=""></div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="">I.e. I want to talk about -2 from the end as a concept without needing to refer to the specific string I’m talking about. &nbsp;If i could generate an index concept without referring to the string itself in anyway, i’d be happy. &nbsp;it’d be wonderful if i could express “-2 in whatever you feel like is the natural unit for the string you’re operating on.”</div><div class=""><br class=""></div><div class="">Does the new API permit me to do that? &nbsp;If not, can I add my own subscript functions which take a new type, and i’ll invent the concept of “index in natural unit of the string being operated on.”</div><div class=""><br class=""></div><div class="">thanks.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Jul 1, 2017 at 12:54 AM, David Baraff <span dir="ltr" class="">&lt;<a href="mailto:davidbaraff@gmail.com" target="_blank" class="">davidbaraff@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><br class=""><div class=""><span class=""><blockquote type="cite" class=""><div class="">On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="m_178362936365510373Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><span class=""></span>Swift's strings were very deliberately designed this way. It's tougher to get off the ground, sure, but it's better in the long run.<span class="m_178362936365510373HOEnZb"></span></div>
<br class=""></blockquote></div><br class=""></div><div class="gmail_extra">It probably is, but the correct idiom is not very well known, and sadly most tutorials and unofficial guides are still teaching dumb ways of subscripting into strings (or worse, falling back into NSString methods without mentioning so) so the end result is people writing less performant code rather than more performant code.<br class=""></div></div></div></blockquote><div class=""><br class=""></div></span>An efficient solution doesn’t help if even experienced programmers can’t easily arrive at it. &nbsp;(I’m highly experienced, but I’ll admit I only put in about 5 minutes before I posted that. &nbsp;on the other hand, it shouldn’t take 5 minutes to figure out something that simple with strings. &nbsp;still, maybe i would have done the simple “suffix()” thing had i been looking at the actual swift 4 api’s, but i only had swift 3 api’s in front of me.)</div><span class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">
______________________________<wbr class="">_________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class=""></div></blockquote></div><br class=""></span></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class="">_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></blockquote></div></body></html>