迅捷使用''对于函数类型,编译器行为与运行时不同

时间:2016-03-23 09:02:27

标签: swift function runtime compiler-warnings

我在Swift中测试function is first class citizen。我遇到了麻烦检查以下代码:

let f: Int -> Int = { $0 }
if f is (Int -> Int?) {  // compiler warning: 'is' test is always true
    print("hello")
} else {
    print("ops")  // runtime: this line is executed
}

我在if行中收到编译器警告说:"'''测试永远是真的"。但是当我运行代码时,控制台打印"操作"这意味着is测试是错误的。这让我很困惑。为什么编译器在运行时声明为false时表示为真?

0 个答案:

没有答案