Quick use of 'is' for function type, compiler behavior is different from runtime

I am testing function is first class citizenin Swift. and I had problems. Check out 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 line if: "test is always true". But when I run the code, the console prints โ€œopsโ€, which means the test isis false. It bothers me. Why does the compiler say true when runtime says false?

0
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1627042/


All Articles