Skip to content

Instantly share code, notes, and snippets.

@jandamm
Last active January 21, 2018 12:12
Show Gist options
  • Save jandamm/2208bf3a0cbd43b7ab1148aa484c19ae to your computer and use it in GitHub Desktop.
Save jandamm/2208bf3a0cbd43b7ab1148aa484c19ae to your computer and use it in GitHub Desktop.
Static vs dynamic dispatch for protocol extensions
protocol A {
func a()
}
extension A {
func a() {
print("protocol extension a")
}
func b() {
print("protocol extension b")
}
}
class X: A {
func a() {
print("class a")
}
func b() {
print("class b")
}
}
class Y: X {
override func a() {
print("subclass a")
}
override func b() {
print("subclass b")
}
}
let x = X()
let y = Y()
let a = x as A
let b = y as A
x.a()
x.b()
y.a()
y.b()
a.a()
a.b()
b.a()
b.b()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment