Avatar
Avatar
Iceman
@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)
1 is none 2 is P 3 is optional and P 4 is none 5 is not P 6 is optional but not P<stdin>:11:20: warning: value 'd' was defined but never used; consider replacing with boolean test [#no-usage] 9 | if let b = a as? any OptionalProtocol { 10 | if let c = b.unwrappedValue { 11 | if let d = c as? any P { | `- warning: value 'd' was defined but never used; consider replacing with boolean test [#no-usage] 12 | print("\(line) is optional and P") 13 | } else { <stdin>:20:16: warning: value 'd' was defined but never used; consider replacing with boolean test [#no-usage] 18 | } 19 | } else { 20 | if let d = a as? any P { | `- warning: value 'd' was defined but never used; consider replacing with boolean test [#no-usage] 21 | print("\(line) is P") 22 | } else { (edited)