String Index unification vs BidirectionalCollection requirements

While I agree that you have highlighted some important issues, I do not see how this claim follows:

The stated requirement that you quote says (paraphrased), “For any index in startIndex ..< endIndex, calling both index(before:) and index(after:) in either order gets you back to the same index where you began.”

It does not say anything about unreachable indices, except that if you start from one and take a step forward and a step backward, you end up at the same (unreachable) index. This is exactly how I would expect unreachable indices to behave.

Are you saying that String violates that?

• • •

The documentation for Collection includes this passage:

That is an explicit statement that unreachable indices are emphatically not valid. Furthermore, the documentation for index(after:), distance(from:to:) and other such operations all include the parameter description:

i: A valid index of the collection.

So the behavior you describe is in fact the direct result of a programmer violating the documented requirements of the methods they are calling.

• • •

The behavior of SE–180 is also quite clear: subscripting with an unreachable (and hence invalid) index is handled by rounding to a valid index.

6 Likes