Why are Swift's range types are not Comparable
, if their element type T
is comparable? (such as Int
, String
, Double
, etc.).
For example:
let rangeFive = 1...10
let rangeSix = 1...10
rangeFive < rangeSix
OR
let rangeFive = 1...10
let rangeSix = 1...10
rangeFive <= rangeSix
OR
let rangeFive = 1...10
let rangeSix = 1...10
rangeFive == rangeSix
let minRangeFive = rangeFive.min()
let maxRangeSix = rangeSix.max()
minRangeFive < maxRangeSix
// Error: Binary operator '<' cannot be applied to two 'ClosedRange<Int>.Element?' (aka 'Optional<Int>') operands
I've tried rangeFive == rangeSix
- and it works, but not with other comparison operators. Why is it that when range types contain Comparable
data, all range types are not Comparable
and do not conform to the Comparable
protocol?
comparable swift ranges are not comparable, i.e. ‘all range types are not comparable, i.e. do not meet the requirements of the Comparable protocol’, while the range data type partialrangefrom, partialrangeupto, partialrangethrought<T>, where T is a comparable data type(Int, String, Double), is comparable.
Making Ranges comparable would lead to more confusion than it's worth, because there's no obviously-correct answer for what it means for one range to be greater than another.
We can agree that 1...2
is definitely less than 3...4
, and that 1...
should probably be considered less than 2...
.
But what about overlapping ranges?
Should 10...20
be less than 10...100
?
There are several ways we could define it, and each of them has issues:
a
is less than range b
if both its lower and upper bounds are both less than b
's upper bounds.
a
is less than range b
only if its lower bound is lower than the lower bound of the other
1...10 < 2...9
would be true (because 1 < 2
), but simultanously 1...10 > 2...9
would be true (because 10 > 9
)."foo"..."bar"
?Equality doesn't have this issue, because you can just compare the bounds, and have an unambiguously correct answer. That's why ranges are conditionally Equatable
, but never Comparable
.