Skip to content

Instantly share code, notes, and snippets.

@koher
Last active August 29, 2015 14:02
Show Gist options
  • Save koher/617a51f3474a9ffc157b to your computer and use it in GitHub Desktop.
Save koher/617a51f3474a9ffc157b to your computer and use it in GitHub Desktop.
Swift's Polymorphism Test
protocol O {
func f() -> String
}
struct A : O {
func f() -> String {
return "a"
}
}
class B : O {
func f() -> String {
return "b"
}
}
//////////////////////////////////////
let a = A()
let b = B()
var o : O
NSLog("%@", a.f()) // a
NSLog("%@", b.f()) // b
o = a
NSLog("%@", o.f()) // a
o = b
NSLog("%@", o.f()) // b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment