Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
Created December 13, 2015 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pyrtsa/4b68c9f0b9a83b4b44fe to your computer and use it in GitHub Desktop.
Save pyrtsa/4b68c9f0b9a83b4b44fe to your computer and use it in GitHub Desktop.
"Equatable Any"
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