[swift-users] Subsequences and shared indices

Hooman Mehr hooman at mac.com
Thu Oct 13 21:49:25 CDT 2016

`Slice` family of types (there are many) are well documented to share the indices and inherit the semantics. All collections that have a SubSequence of a Slice type, share indices. Unfortunately, standard library is not well documented in general and collection API have undergone big changes in Swift 3.0, so this may not be very clear now. 

The problem with `String.CharacterView` is that its SubSequence is not a `Slice` type.

Try this out:

let subSeq1 = "abc".characters.prefix(1)
let subSeq2 = Array("abc".characters).prefix(1)

print(type(of:subSeq1)) // prints CharacterView
print(type(of:subSeq2)) // prints ArraySlice<Character>

Converting `String.CharacterView` to Array<Character> is an expensive operation and will kill the performance of string manipulation, but will work as you expect, since Array is properly sliced.

> On Oct 13, 2016, at 5:42 PM, Tim Vermeulen <tvermeulen at me.com> wrote:
> Alright. Does this mean that we can otherwise assume that collections share indices with their subsequences? It might be worth documenting, one way or the other.
>> On 14 Oct 2016, at 02:40, Hooman Mehr <hooman at mac.com <mailto:hooman at mac.com>> wrote:
>> This is a bug reported multiple times in different forms. My version of it is: SR-1487 <https://bugs.swift.org/browse/SR-1487>. 
>> It remains open because it is not easy to fix with the existing design of String. Apparently core standard library team are working on an overhaul of String to address this and other usability and performance issues.
>>> On Oct 13, 2016, at 5:12 PM, Tim Vermeulen via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>>> Is it a requirement that collections share indices with its subsequence? Array and ArraySlice do share indices, which is why ArraySlice isn’t zero-based, and I think this is convenient. But String.CharacterView doesn’t seem to share indices with its subsequence (which is String.CharacterView as well). Consider this example:
>>> let foo = "foobar".characters
>>> let index = foo.index(foo.startIndex, offsetBy: 3)
>>> let bar = foo.suffix(from: index)                   // "bar"
>>> foo[index]                                          // "b" :)
>>> foo[bar.startIndex]                                 // "f" :(
>>> So does this mean that we can’t assume that collections and their subsequences share their indices (which could be very handy),  or is this just a bug related to String.CharacterView?
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161013/f6f14d4c/attachment.html>

More information about the swift-users mailing list