Avatar
@swift-6.2.3 detect(a: Int?.none as Any) detect(a: 1 as Any) detect(a: Int?.some(1) as Any) detect(a: String?.none as Any) detect(a: "hoge" as Any) detect(a: String?.some("hoge") as Any) func detect(a: Any, line: Int = #line) { if let b = a as? any OptionalProtocol { if let c = b.unwrappedValue { if let d = c as? any P { print("\(line) is optional and P") } else { print("\(line) is optional but not P") } } else { print("\(line) is none") } } else { if let d = a as? any P { print("\(line) is P") } else { print("\(line) is not P") } } } protocol P {} extension Int: P {} protocol OptionalProtocol<Wrapped> { associatedtype Wrapped var unwrappedValue: Wrapped? { get } } extension Optional: OptionalProtocol { var unwrappedValue: Wrapped? { switch self { case .some(let value): return value case .none: return nil } } } (edited)