Created
December 13, 2015 15:26
-
-
Save pyrtsa/4b68c9f0b9a83b4b44fe to your computer and use it in GitHub Desktop.
"Equatable Any"
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
struct Eq : Equatable { | |
let unbox: Any | |
let equal: Any -> Bool | |
init(value: Any, equal: Any -> Bool) { | |
self.unbox = value | |
self.equal = equal | |
} | |
init<T : Equatable>(_ value: T) { | |
self.init(value: value) { any in | |
guard let other = any as? T else { return false } | |
return value == other | |
} | |
} | |
} | |
func == (a: Eq, b: Eq) -> Bool { return a.equal(b.unbox) } | |
Eq(1) == Eq(2) // false | |
Eq(1) == Eq(1) // true | |
Eq(1) == Eq(1.0) // false | |
Eq("one") == Eq("one") // true | |
Eq("one") == Eq(1) // false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment