Swift use 'is' for function type, compiler behavior is different with runtime

大憨熊 提交于 2019-12-23 04:45:10

问题


I am testing about function is first class citizen in Swift. and I got in trouble. Check the code below:

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
}

I got a compiler warning in the if line says: "'is' test is always true". But when I run the code, console print "ops" which means is test is false. This confuses me. Why compiler says true when runtime says false?

来源:https://stackoverflow.com/questions/36173726/swift-use-is-for-function-type-compiler-behavior-is-different-with-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!