Subscripting a string should be possible or have an easy alternative

I'll not leave this thread to wither on the vine but summarise my own conclusions.

It was informative for me to develop the "String16" type I mentioned as it helped firm up my evaluation of Swift's string abstraction which is that it is essentially sound. It was probably a mistake to introduce it into this thread though as people started to fixate on its simple utf-16 data representation as if that was all it was. In combination with a smart index type and tapping into the "ICU" it can be a Unicode correct as you like. For all-BMP strings it does give you an entry level api where integer indexes are character indexes.

But this was all by the by as the real problem Swift really needs to solve is the difficulty working with the String.Index type. The StringIndex package I introduced solves this in a simplistic, performant and correct manner. You can't use integer subscripts with String but relative ones such as .start+n which is to my mind a sufficient reminder to users this is something a little different and an O(n) operation. No feet were harmed in the making of this package.

The similar formal proposal SE-0265 goes further and looks to upstream this behaviour into the collection protocols which seems more difficult than it needs to be and perhaps as a result 5 years later we are still waiting for it to come back to review. This was all discussed in my original thread way back then and we are no further along.

I don't think it's an exaggeration to say this is the #1 thing that needs to be fixed in Swift to improve many people's initial impression of the language. We have a stack overflow answer on indexing viewed 500,000 times which means practically all Swift programers have had to consult it at least once. I don't know what more I can do to try to inject urgency into this conversation other than swear or some other minor code violation. The thread title phrases the still unanswered question perfectly "Subscripting a string should be possible or have an easy alternative". Solving this problem in a timely manner is not beyond the combined wit of the people represented here but it seems we're choosing not to solve it to "teach people a lesson".

1 Like