[swift-users] Subsequences and shared indices

Hooman Mehr hooman at mac.com
Sun Oct 16 21:31:13 CDT 2016

Thanks to Ole Begemann (who closed my bug report), I found out that Nate Cook submitted a fix <https://github.com/apple/swift/pull/4896> for this issue a few day ago. The fix will hopefully make it to the next maintenance release of Swift.

The documentation of range subscript (which is the main API for creating subsequences) already specifies that SubSequences (slices) should use the same indices, but I think we need to better document the specifications of SubSequence associated type and make sure that any API that returns a SubSequence links to SubSequence documentation.

> On Oct 16, 2016, at 3:33 PM, Dave Abrahams via swift-users <swift-users at swift.org> wrote:
> on Thu Oct 13 2016, Tim Vermeulen <swift-users-AT-swift.org> wrote:
>> Is it a requirement that collections share indices with its
>> subsequence? 
> Yes.
>> 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). 
> That's a bug.
>> 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?
> The latter.
> -- 
> -Dave
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

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

More information about the swift-users mailing list