For Collection.distance(from: to:), why must precondition(start <= end)?

Looking at it now, you would think that

guard start <= end else { return -distance(from: end, to: start) }

wouldn't be that hard to do. Is this a remnant of when IndexDistance was selectable? More importantly, was it allowed to be an unsigned integer (or anything else that couldn't be reliably negated)?

distance(to:) is paired with index(_:offsetBy:) .

If the type conforms to BidirectionalCollection , then you can have negative distances.

If it doesn’t, then a negative distance is semantically problematic, as you cannot traverse a collection in that direction.

2 Likes
Terms of Service

Privacy Policy

Cookie Policy