Skip to content

Commit b8cbd4f

Browse files
committed
[stdlib][docs] Clarify BidirectionalCollection requirements
As discussed on the forum, `BidirectionalCollection` requirements as currently stated can be interpreted to forbid conforming types from accepting indices that lie between valid (i.e., reachable) indices in the collection. Among other undesirable effects, this interpretation would render SE-0180 (String.Index overhaul) incompatible with these requirements. Update the wording to clarify that the requirements only apply to valid indices. (Collection protocols do not constrain the behavior of invalid indices — it’s up to individual collection types to implement them as they wish.) https://forums.swift.org/t/string-index-unification-vs-bidirectionalcollection-requirements/55946 rdar://92297280
1 parent 565ed69 commit b8cbd4f

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@
2828
/// `Collection` protocol.
2929
///
3030
/// Indices that are moved forward and backward in a bidirectional collection
31-
/// move by the same amount in each direction. That is, for any index `i` into
32-
/// a bidirectional collection `c`:
31+
/// move by the same amount in each direction. That is, for any valid index `i`
32+
/// into a bidirectional collection `c`:
3333
///
3434
/// - If `i >= c.startIndex && i < c.endIndex`, then
3535
/// `c.index(before: c.index(after: i)) == i`.
3636
/// - If `i > c.startIndex && i <= c.endIndex`, then
3737
/// `c.index(after: c.index(before: i)) == i`.
38+
///
39+
/// Valid indices are exactly those indices that are reachable from the
40+
/// collection's `startIndex` by repeated applications of `index(after:)`, up
41+
/// to, and including, the `endIndex`.
3842
public protocol BidirectionalCollection: Collection
3943
where SubSequence: BidirectionalCollection, Indices: BidirectionalCollection {
4044
// FIXME: Only needed for associated type inference.

0 commit comments

Comments
 (0)