Repeating protocol conformance in a subclass of conforming class results in a redundancy error. My problem is that, because of how protocol witness tables work, repeating protocol conformance in a subclass seems not to be redundant at all . Intuitively, in the Example3
below repeating a conformance of the parent class in the child class could restore the route from protocol extension's implementation back to child's implementation . Where am I wrong?
protocol Prot {
func f()
func g()
}
extension Prot {
func f(){ print("protocol extension's implementation") }
func g(){ f() }
}
class Parent: Prot {
}
//Directly implementing protocol would route to a child's implementation.
class Example1: Prot {
func f(){ print("child's implementation")}
}
//Indirectly implementing protocol would route to a protocol extension's implementation.
class Example2: Parent {
func f(){ print("child's implementation")}
}
//Redundant conformance of 'Child' to protocol 'Prot' error, instead of restoring route to a child's implementation.
class Example3: Parent, Prot {
func f(){ print("child's implementation")}
}
Example1().g()
Example2().g()
From Kavon F(Apple), during Ask Apple
So, this interaction with default protocol witnesses has been reported before here. If you want the ability to override the
Parent
class's default witness to thef()
requirement that was provided byProt,
theParent
, who is the actual conformer of the protocol, currently must provide its own witness so that the subclass (e.g.,Example3
) can use a standard methodoverride
to change theParent
behavior after the fact. Protocols don't support inheritance, so they're choosing the methods directly listed in the type that wants to conform (or using defaults). Since a subclass can always be substitued in places where the superclass type is expected, the subclass is considered to be already conforming to the protocol. Thus, the conformance listed in the subclass is currently considered meaningless. It could mean that, and to make it happen, a pitch on Swift Evolution would be a way to start the conversation.