Skip to content

Commit 3ae3a7b

Browse files
authored
Restore documentation for the pattern-matching operator as applied to RangeExpressions
The previous documentation of this operator in `Range` context was lost sometime during 2017. This new version is a simplified version of the original copy, with substantial inspiration taken from the present-day documentation of the same operator on `Optional`. Fixes [SR-12842](https://bugs.swift.org/browse/SR-12842).
1 parent fe0bd4f commit 3ae3a7b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

stdlib/public/core/Range.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ public protocol RangeExpression {
7474
}
7575

7676
extension RangeExpression {
77+
78+
/// Returns a Boolean value indicating whether a value is included in a
79+
/// range.
80+
///
81+
/// You can use the pattern-matching operator (`~=`) to test whether a value
82+
/// is included in a range. The pattern-matching operator is used
83+
/// internally in `case` statements for pattern matching. The following
84+
/// example uses the `~=` operator to test whether an integer is included in
85+
/// a range of single-digit numbers:
86+
///
87+
/// let chosenNumber = 3
88+
/// if 0..<10 ~= chosenNumber {
89+
/// print("\(chosenNumber) is a single digit.")
90+
/// }
91+
/// // Prints "3 is a single digit."
92+
///
93+
/// - Parameters:
94+
/// - pattern: A range.
95+
/// - bound: A value to match against `pattern`.
7796
@inlinable
7897
public static func ~= (pattern: Self, value: Bound) -> Bool {
7998
return pattern.contains(value)

0 commit comments

Comments
 (0)