Skip to content

Instantly share code, notes, and snippets.

@amosavian
Created February 27, 2018 13:08
Show Gist options
  • Save amosavian/39c2a1d1fcdbaf3747c737062bbcbe0d to your computer and use it in GitHub Desktop.
Save amosavian/39c2a1d1fcdbaf3747c737062bbcbe0d to your computer and use it in GitHub Desktop.
protocol A {
func foo() -> Int
}
extension A {
func foo() -> Int {
return 10
}
}
class BaseOne: A {
func foo() -> Int {
return 20
}
}
class ChildOne: A {
func foo() -> Int {
return 200
}
}
class BaseTwo: A {
}
class ChildTwo: BaseTwo {
func foo() -> Int {
return 30
}
}
BaseOne().foo() // Print 20
(BaseOne() as A).foo() // Print 20
ChildOne().foo() // Print 200
(ChildOne() as A).foo() // Print 200
ChildTwo().foo() // Print 30
(ChildTwo() as A).foo() // Print 10 !!!! (Must be 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment