Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save diegosanchezr/607fb1a70193028815f4 to your computer and use it in GitHub Desktop.
Save diegosanchezr/607fb1a70193028815f4 to your computer and use it in GitHub Desktop.
Protocol extensions unexpected implementation
protocol MyProtocol {
func doSomething()
}
extension MyProtocol {
func doSomething() {
print("default impl")
}
}
class A: MyProtocol {}
class B: A {
func doSomething() {
print("B impl")
}
}
let b: MyProtocol = B()
b.doSomething() // Prints "default impl"
////
protocol MyProtocol {
func doSomething()
}
extension MyProtocol {
func doSomething() {
print("default impl")
}
}
class A: MyProtocol {
func doSomething() {
print("A impl")
}
}
class B: A {
override func doSomething() {
print("B impl")
}
}
let b: MyProtocol = B()
b.doSomething() // Prints "B impl"
@diegosanchezr
Copy link
Author

Xcode 7.2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment