Last active
July 31, 2018 11:11
-
-
Save mattt/7e4db72ce1b6c8a18bb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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