Type of optionals cannot be inferred correctly in swift 2.2

前端 未结 1 373
我在风中等你
我在风中等你 2021-01-17 09:36

Problem:

When running the following code under Xcode 7.3 with swift 2.2, the compiler is unable to correctly infer the type of the optional:

相关标签:
1条回答
  • 2021-01-17 10:34

    This seems to be a bug. The minimal example is the following:

    func genericMethod<T>(property: T?) {
        print(T) // String
    
        let stringNil = Optional<String>.None
    
        print(stringNil is String?) // true (warning - always true)    
        print(stringNil is T?) // true
    
        let intNil = Optional<Int>.None
    
        print(intNil is String?) // false (warning - always fails)
        print(intNil is T?) // true - BUG
    }
    
    genericMethod("")
    
    0 讨论(0)
提交回复
热议问题