I don\'t think this can be done but I\'ll ask anyway. I have a protocol:
protocol X {}
And a class:
class Y:X {}
Not sure why you need all instances of your protocol to conform to Equatable
, but I prefer letting classes implement their equality methods.
In this case, I'd leave the protocol simple:
protocol MyProtocol {
func doSomething()
}
If you require that an object that conforms to MyProtocol
is also Equatable
you can use MyProtocol & Equatable
as type constraint:
// Equivalent: func doSomething(element1: T, element2: T) where T: MyProtocol & Equatable {
func doSomething(element1: T, element2: T) {
if element1 == element2 {
element1.doSomething()
}
}
This way you can keep your specification clear and let subclasses implement their equality method only if required.