I’ll answer my own question (fired up xcode beta to try some stuff in swift 4):

> 
>  Is there a way of writing something looks like
> 
>       getLongergDeviceId()[<-2 from end> ... ]
> 
> where whatever goes in the <-2 from the end> does NOT refer to 
> getLongerDeviceID()?  

and in swift 4, the answer is “yes.”  If I simply overload the string subscript 
operator to operate on CountablePartialRange<Int>, 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:

        let shortDeviceId = getLongerDeviceID[(-2)...]

         let someOtherSlice = getLongerDeviceID[3...]   // start at 3 from 
beginning and go to end

So *I’ll* be happy.  I just need to overload to handle all the different range 
types.  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.

I could imagine, just to avoid amiguity, perhaps

         let someOtherSlice = getLongerDeviceID[UTF8.Index(3)...]       // 
start at 3 from beginning and go to end

which wouldn’t be the end of the world (compared with what we have now).


> 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.  If i could generate an index 
> concept without referring to the string itself in anyway, i’d be happy.  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.”
> 
> Does the new API permit me to do that?  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.”
> 
> thanks.
> 
> 
> 
> 
> 
> 
>> 
>> On Sat, Jul 1, 2017 at 12:54 AM, David Baraff <davidbar...@gmail.com 
>> <mailto:davidbar...@gmail.com>> wrote:
>> 
>>> On Jun 30, 2017, at 9:48 PM, Taylor Swift via swift-users 
>>> <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
>>> 
>>> 
>>> 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.
>>> 
>>> 
>>> 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.
>> 
>> An efficient solution doesn’t help if even experienced programmers can’t 
>> easily arrive at it.  (I’m highly experienced, but I’ll admit I only put in 
>> about 5 minutes before I posted that.  on the other hand, it shouldn’t take 
>> 5 minutes to figure out something that simple with strings.  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.)
>> 
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users@swift.org <mailto:swift-users@swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users 
>>> <https://lists.swift.org/mailman/listinfo/swift-users>
>> 
>> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to