I don't think you will find a "simpler" or "shorter" way of writing this but you can define your own pattern matching operator to shorten your code:
extension Range {
static func ~=(lhs: Self, rhs: Self) -> Bool {
rhs.clamped(to: lhs) == rhs
}
}
extension ClosedRange {
static func ~=(lhs: Self, rhs: Self) -> Bool {
rhs.clamped(to: lhs) == rhs
}
}
let smallRange = 1..<2
let largeRange = 0..<10
smallRange ~= largeRange // false
largeRange ~= smallRange // true
Note: It compiles because there is a generic implementation used to check for equality like switching a string therefore it would only return true if both ranges were the same.
func ~= <T>(a: T, b: T) -> Bool where T : Equatable