[swift-users] Subsequences and shared indices
Dave Abrahams
dabrahams at apple.com
Sun Oct 16 17:33:06 CDT 2016
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
More information about the swift-users
mailing list