I use reflection and I need to test if certain members of a type are represented by a generic type. The information I want to get does not require knowing a generic signature.
How can I test types for equality?
struct A { var a: Optional<Int> }
let mirror = Mirror.init(reflecting: A(a: .some(8)))
for member in mirror.children {
type(of:member.value) == Optional<_>.self // how to do this? Any doesn't work
}
While you cannot do it directly, you can cheat by making a private protocol and making all Optional types conform to it
private protocol OptionalProtocol {}
extension Optional: OptionalProtocol {}
struct A { var a: Optional<Int> }
let mirror = Mirror.init(reflecting: A(a: .some(8)))
for member in mirror.children {
print(member.value is OptionalProtocol) // true
}