Please consider the following Swift 5 code:
protocol P: class {
func call_foo()
func foo()
func call_bar()
func bar()
}
extension P {
func call_foo() { foo() }
func foo() { print("P.foo") }
func call_bar() { bar() }
func bar() { print("P.bar") }
}
class C1: P {
func foo() { print("C1.foo") }
}
class C2: C1 {
func bar() { print("C2.bar") }
}
let c = C2()
c.call_foo() // C1.foo
c.foo() // C1.foo
c.call_bar() // P.bar
c.bar() // C2.bar
If the foo()
call in P.call_foo()
gets dynamically dispatched to C1.foo()
, then why the bar()
call in P.call_bar()
does not get dynamically dispatched to C2.bar()
?
The only difference is that foo()
is overridden directly in the class that conforms to P
, and bar()
is only overridden in a subclass. Why does that make a difference?
Given that bar()
is a protocol requirement, shouldn't all calls to it always get dynamically dispatched?