Last active
January 21, 2018 12:12
-
-
Save jandamm/2208bf3a0cbd43b7ab1148aa484c19ae to your computer and use it in GitHub Desktop.
Static vs dynamic dispatch for protocol extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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