[swift-evolution] Strings in Swift 4

Saagar Jha saagar at saagarjha.com
Sat Feb 4 03:44:30 CST 2017


Sorry, it looks like I left you hanging on this–luckily I found it when I was cleaning my inbox.

Overall, I believe the issue I have with the Swift String indexing model is that indices cannot be operated on like an Int can–you can multiply, divide, square, whatever you want on integer indices, while String.Index only allows for what is essentially addition and subtraction. Now, I get that these operations may not make sense on most Strings; the existing API covers them well. However, there are cases, where these operations would be convenient; such as when dealing with fixed-length records or tables of data; almost invariably these are stored as ASCII. Thus, for these cases, I believe that there should be some way to let String know that we are dealing with something that is purely ASCII, so that it can allow us to use these operations in an efficient manner (for example, having an optional .asciiString property that conforms to RandomAccess; since I don’t believe that extendedASCII does). Such an API would keep the existing String paradigm, which is what is needed most of the time, but allowing for random access when the data can be guaranteed to support it.

I’m not sure if I’m getting my point across, please do let me know if you don’t quite get what I mean.

Saagar Jha

> On Jan 20, 2017, at 5:55 PM, Ben Cohen <ben_cohen at apple.com> wrote:
> 
> 
>> On Jan 20, 2017, at 2:58 PM, Saagar Jha via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> Sorry if I wasn’t clear; I’m looking for indexing using Int, instead of using formIndex.
> 
> 
> Question: why do you think integer indices are so desirable?
> 
> Integer indexing is simple, but also encourages anti-patterns (tortured open-coded while loops with unexpected fencepost errors, conflation of positions and distances into a single type) and our goal should be to make most everyday higher-level operations, such as finding/tokenizing, so easy that Swift programmers don’t feel they need to resort to loops as often.
> 
> Examples where formIndex is so common yet so cumbersome that it would be worth efforts to create integer-indexed versions of string might be indicators of important missing features on our collection or string APIs. So do pass them along.
> 
> (There are definitely known gaps in them today – slicing needs improving as the manifesto mentions for things like slices from an index to n elements later. Also, we need support for in-place remove(where:) operations. But the more commonly needed cases we know about that aren’t covered, the better)
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170204/1efb226f/attachment.html>


More information about the swift-evolution mailing list