Search code examples
swiftswift-protocolsprotocol-extension

Are mixed class/protocol type constraints allowed in Swift protocol extensions?


Is it possible, in any version of Swift, to extend a protocol with mixed class/protocol type constraints? For example, I would like to extend Protocol2 only when Self is a subclass of UIViewController and conforms to Protocol1.

protocol Protocol1 {}
protocol Protocol2 {}

//What I imagine I could do, but it does not compile
extension Protocol2 where Self: UIViewController, Protocol2 {}

Solution

  • Try:

    extension Protocol2 where Self: UIViewController, Self: Protocol2 {}