5

I read the documentation about Protocols and can't find any information about protocol inheriting from a class, but the code compiles. As far as I remember protocols can only inherit other protocols, I have never seen protocol that inherits from a class. I don't even know a language that allows such behaviour.

class A { } protocol X: A { } // forced to inherit from class A, because of X protocol class B: A, X { } 

Is it some kind of bug?

1
  • I think it's the same as protocol X where Self:A and that this short way to write it has been added recently (Swift 5?). Commented Nov 14, 2019 at 9:20

1 Answer 1

17

Implemented In Swift 5: From the Swift 5 Release notes

Protocols can now constrain their conforming types to those that subclass a given class. Two equivalent forms are supported:

protocol MyView: UIView { /*...*/ } protocol MyView where Self: UIView { /*...*/ } 

See this tweet by John Sundell, showing a possible use case

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.