Since the range method already works fine, I don’t imagine it would ever be accepted into the Standard Library.
That said, it is easy to implement yourself in a library—well almost. In theory all the building blocks are available. The only hitch is that the Standard Library gets in the way by already defining the comparison operators with no associativity. You cannot shadow a Standard Library operator with your own, nor can you opt out of importing the Standard Library to evade the name collision.
You can get close if you are willing to put up with parentheses:
func <(lhs: Int, rhs: Int) -> (veracity: Bool, checkedValue: Int) {
return ((lhs < rhs) as Bool, rhs)
}
func <=(lhs: (veracity: Bool, checkedValue: Int), rhs: Int) -> Bool {
return lhs.veracity && (lhs.checkedValue <= rhs)
}
let numberOfApples = 5
if (0 < numberOfApples) <= 10 {
// ...
}
Or you can move to your own operators in order to drop the parentheses. Hurray for Unicode!
precedencegroup Inequality {
lowerThan: ComparisonPrecedence
associativity: left
}
infix operator ≤: Inequality
func <(lhs: Int, rhs: Int) -> (veracity: Bool, checkedValue: Int) {
return ((lhs < rhs) as Bool, rhs)
}
func ≤(lhs: (veracity: Bool, checkedValue: Int), rhs: Int) -> Bool {
return lhs.veracity && (lhs.checkedValue <= rhs)
}
let numberOfApples = 5
if 0 < numberOfApples ≤ 10 {
// ...
}
(Unicode can only properly help you fix x < y ≤ z
, x ≤ y < z
, and x ≤ y ≤ z
. You cannot express x < y < z
unless you invent some more exotic replacement character—such as the “precedes” symbol: ≺
.)