Skip to content

Instantly share code, notes, and snippets.

@mattt
Last active July 31, 2018 11:11
Show Gist options
  • Save mattt/7e4db72ce1b6c8a18bb4 to your computer and use it in GitHub Desktop.
Save mattt/7e4db72ce1b6c8a18bb4 to your computer and use it in GitHub Desktop.
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