Skip to content

Instantly share code, notes, and snippets.

@pcantrell
Created June 15, 2017 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcantrell/0c2f27defd23f80f26d7bb1cf2cc9694 to your computer and use it in GitHub Desktop.
Save pcantrell/0c2f27defd23f80f26d7bb1cf2cc9694 to your computer and use it in GitHub Desktop.
protocol A {
func foo()
}
extension A {
func foo() { print("A foo") }
func bar() { print("A bar") }
}
protocol B: A {
func bar()
}
extension B {
func foo() { print("B foo") }
func bar() { print("B bar") }
}
class C: B { }
let c: B = C()
print("Static type B:")
c.foo() // prints "B foo"
c.bar() // prints "B bar"
print("Static type A:")
(c as A).foo() // prints "B foo"
(c as A).bar() // prints "A bar" !!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment