@swift-5.1.5
public protocol MaybeEquatable { func eraseToOpaqueEquatable() -> ErasedOpaqueEquatable } extension MaybeEquatable { public func isEqual(to other: Any) -> Bool { eraseToOpaqueEquatable().isEqualTo(other) } } extension MaybeEquatable { public func eraseToOpaqueEquatable() -> ErasedOpaqueEquatable { ErasedOpaqueEquatable() } } extension MaybeEquatable where Self: Equatable { public func eraseToOpaqueEquatable() -> ErasedOpaqueEquatable { ErasedOpaqueEquatable(value: self) } } public struct ErasedOpaqueEquatable { var isEqualTo: (Any) -> Bool init() { isEqualTo = { _ in false } } init<T: Equatable>(value: T) { isEqualTo = { value == $0 as? T } } } // 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