Avatar
あ、ダメだった
3:14 PM
@swift-5.1.5 protocol AnyEquatableBase { associatedtype Value var value: Value { get } func isEqual(to other: Self) -> Bool } extension AnyEquatableBase { func isEqual(to other: Self) -> Bool { false } } extension AnyEquatableBase where Value: Equatable { func isEqual(to other: Self) -> Bool { value == other.value } } protocol MaybeEquatable { } extension MaybeEquatable { func isEqual(to other: Self) -> Bool { AnyEquatable(value: self).isEqual(to: AnyEquatable(value: other)) } } struct AnyEquatable<T>: AnyEquatableBase { var value: T } extension String: MaybeEquatable {} func check<T: MaybeEquatable>(lhs: T, rhs: T) { print(lhs.isEqual(to: rhs)) } func check2(lhs: String, rhs: String) { print(AnyEquatable(value: lhs), AnyEquatable(value: lhs).isEqual(to: AnyEquatable(value: rhs))) } check(lhs: "test", rhs: "test") check2(lhs: "test", rhs: "test")