Yes. That would also match Equatable, Hashable & Codable conditional conformances.
Possible implementation
extension Array: Comparable where Element: Comparable {
public static func < (lhs: Array<Element>, rhs: Array<Element>) -> Bool {
let lcount = lhs.count
let rcount = rhs.count
for i in 0 ..< Swift.min(lcount, rcount) {
if lhs[i] != rhs[i] {
return lhs[i] < rhs[i]
}
}
return lcount < rcount
}
}
extension Optional: Comparable where Wrapped: Comparable {
public static func < (lhs: Optional, rhs: Optional) -> Bool {
if let left = lhs {
if let right = rhs {
return left < right
} else {
return false
}
} else {
return rhs != nil
}
}
}
Do you use this in your project and does it it work well? I'm trying to imagine a situation when Array/Optional conforming to Comparable could be harmful but struggling to find an example.
Worth considering Set and Dictionary as well.