Last active
January 7, 2020 04:59
-
-
Save tarunon/85bdeccf8bc8f44b7b27d4974e0a843f 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
public typealias PartialApplySelfToIsEqual = (Any?) -> Bool | |
public protocol MaybeEquatable { | |
func partialApplySelftoIsEqual() -> PartialApplySelfToIsEqual | |
} | |
extension MaybeEquatable { | |
public func partialApplySelftoIsEqual() -> PartialApplySelfToIsEqual { | |
{ _ in false } | |
} | |
} | |
extension MaybeEquatable where Self: Equatable { | |
public func partialApplySelftoIsEqual() -> PartialApplySelfToIsEqual { | |
{ self == $0 as? Self } | |
} | |
} | |
extension MaybeEquatable { | |
public func isEqual(to other: Any?) -> Bool { | |
self.partialApplySelftoIsEqual()(other) | |
} | |
} | |
// end user interface | |
public func anyIsEqual(_ a: Any, _ b: Any) -> Bool { | |
guard let a = a as? MaybeEquatable else { return false } | |
return a.isEqual(to: b) | |
} | |
// you need to mark your type | |
extension String: MaybeEquatable {} | |
print(anyIsEqual("str", "str")) // => true | |
print(anyIsEqual({}, {})) // => false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @omochi https://gist.github.com/omochi/b68706b90134db8c3771195da170d4b5