SwiftQuiz
#7

Function Subtyping

  • functions
  • inheritance
Swift 6.3.2 Report an issue

Verified quizzes have their expected answer checked by compiling and running the code with the listed Swift toolchain during the site build.

class A {}
class B: A {}
var f: (A) -> B = { _ in B() }
var g: (B) -> A = f
print(type(of: g(B())))
Hint

Swift permits the assignment g = f because function types are contravariant in their parameters and covariant in their result. Which way does each direction go here?

Your answer

What does this program do?