SwiftQuiz
#13

Conditional Default

  • conditional-conformance
  • protocols
  • dispatch
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.

protocol P {}
extension P {
    func value() -> Int {
        1
    }
}

extension P where Self: Equatable {
    func value() -> Int {
        2
    }
}

struct S: P, Equatable {}
struct T: P {}

print(S().value())
print(T().value())
Hint

Both extensions define value(). Each conforming type sees zero, one, or both of them depending on the constraint clause. What does the compiler pick when both are visible?

Your answer

What does this program do?