Create a gist now

Instantly share code, notes, and snippets.

enum ComparisonOrdering: Int {
case Ascending = 1
case Descending = -1
case Same = 0
}
infix operator <=> { precedence 130 }
protocol CombinedComparable: Comparable, Equatable {
func <=>(lhs: Self, rhs: Self) -> ComparisonOrdering
}
func <<T: CombinedComparable>(lhs: T, rhs: T) -> Bool {
return (lhs <=> rhs) == .Ascending
}
func ==<T: CombinedComparable>(lhs: T, rhs: T) -> Bool {
return (lhs <=> rhs) == .Same
}
///////////
struct X: CombinedComparable {
let value: Int
}
func <=>(lhs: X, rhs: X) -> ComparisonOrdering {
if lhs.value < rhs.value {
return .Ascending
} else if lhs.value > rhs.value {
return .Descending
} else {
return .Same
}
}
let a = X(value: 0)
let b = X(value: 1)
a == b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment