Skip to content

Instantly share code, notes, and snippets.

@nahive
Created April 21, 2017 03:21
Show Gist options
  • Save nahive/7fa924ba79ce6b226156b39015f46b85 to your computer and use it in GitHub Desktop.
Save nahive/7fa924ba79ce6b226156b39015f46b85 to your computer and use it in GitHub Desktop.
extension Comparable {
/// Returns a Boolean value indicating whether a value is included in a
/// range.
///
/// You can use this pattern matching operator (`~=`) to test whether a value
/// is included in a range. The following example uses the `~=` operator to
/// test whether an integer is included in a range of single-digit numbers.
///
/// let chosenNumber = 3
/// if chosenNumber ~= 0...9 {
/// print("\(chosenNumber) is a single digit.")
/// }
/// // Prints "3 is a single digit."
///
/// - Parameters:
/// - lhs: A value to match against `rhs`.
/// - rhs: A range.
public static func ~=(lhs: Self, rhs: ClosedRange<Self>) -> Bool {
return rhs ~= lhs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment