[swift-evolution] Pitch: String Index Overhaul

Dave Abrahams dabrahams at apple.com
Sat Jun 3 12:40:05 CDT 2017


Philippe Hausler via swift-evolution
<swift-evolution at swift.org> wrote:
> I would presume that the index type will still be shared between String and SubString, 

Yes; that's a requirement of Collection conformance. 

> will this mean that we will now be able to express index manipulation in StringProtocol?

Depends what you mean by that I guess ;-)

> I find StringProtocol a bit hard to deal with when attempting to make range conversions;

StringProtocol is pretty far from being in its indented final form, if
that's any consolation…

> it would be really nice if we could make this possible (or perhaps more
> intuitive... since, for the life of me I can’t figure out a way to
> generically convert indexes for StringProtocol adoption)
> 
> So lets say you have a function as such: 
> 
> func foo<S: StringProtocol>(_ str: S, range: Range<S.Index>) {
>     range.lowerBound.samePosition(in: str.utf16)
> }
> 
> results in the error error: value of type 'S.Index' has no member ‘samePosition’

> This of course is an intended target of something that deals with strings
> and wants to deal with both strings and substrings uniformly since it is
> reasonable to pass either.
> 
> In short: are StringProtocol accessors a consideration for conversion in this change?

I'm sorry but I don't understand the question. If you're asking whether
this particular proposal is designed to address index translation on models
of StringProtocol, the answer is no. It will appear to do so for the moment
but only because StringProtocol is currently over-constrained. 

The intention is that eventually StringProtocol does not constrain its
views to all have the same index type, but does constrain them to
Unicode.ViewIndex, which will require the encodedOffset initializer and
property.  Also the index type of a StringProtocol's SubSequence's XXXView
will be constrained to be the same as that of the StringProtocol's own
XXXView. 

Hope this helps,
Dave 



More information about the swift-evolution mailing list