Skip to content

Commit f786a9a

Browse files
committed
[Standard library] Duplicate subscript requirement in BidirectionalCollection.
The BidirectionalCollection protocol has no requirements that mention the associated type 'SubSequence', which means it cannot be easily inferred. Duplicate the subscript requirement from Collection into BidirectionalCollection to aid type witness inference.
1 parent 6f36235 commit f786a9a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

stdlib/public/core/BidirectionalCollection.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ public protocol BidirectionalCollection
134134
///
135135
/// - Complexity: O(1)
136136
var last: Iterator.Element? { get }
137+
138+
/// Accesses a contiguous subrange of the collection's elements.
139+
///
140+
/// The accessed slice uses the same indices for the same elements as the
141+
/// original collection uses. Always use the slice's `startIndex` property
142+
/// instead of assuming that its indices start at a particular value.
143+
///
144+
/// This example demonstrates getting a slice of an array of strings, finding
145+
/// the index of one of the strings in the slice, and then using that index
146+
/// in the original array.
147+
///
148+
/// let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
149+
/// let streetsSlice = streets[2 ..< streets.endIndex]
150+
/// print(streetsSlice)
151+
/// // Prints "["Channing", "Douglas", "Evarts"]"
152+
///
153+
/// let index = streetsSlice.index(of: "Evarts") // 4
154+
/// print(streets[index!])
155+
/// // Prints "Evarts"
156+
///
157+
/// - Parameter bounds: A range of the collection's indices. The bounds of
158+
/// the range must be valid indices of the collection.
159+
subscript(bounds: Range<Index>) -> SubSequence { get }
137160
}
138161

139162
/// Default implementation for bidirectional collections.

0 commit comments

Comments
 (0)