Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save grigorye/2c491047ce04cf8002b2b3775a162a2e to your computer and use it in GitHub Desktop.
Save grigorye/2c491047ce04cf8002b2b3775a162a2e to your computer and use it in GitHub Desktop.
protocol P {
func foo()
}
extension P {
func foo() { print("P") }
}
class A: P {}
class B: A {
func foo() { print("B") } // <- Does not work.
}
class C: P {
func foo() { print("C") }
}
class D: C {
override func foo() { print("D") }
}
let instances: [P] = [A(), B(), C(), D()]
instances.forEach { $0.foo() }
/* Actual output:
P
P
C
D
*/
/* Desired output:
P
B
C
D
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment