Skip to content

Instantly share code, notes, and snippets.

@wjlafrance
Created October 22, 2014 22:46
Show Gist options
  • Save wjlafrance/2ed930fc69e089d3a1b2 to your computer and use it in GitHub Desktop.
Save wjlafrance/2ed930fc69e089d3a1b2 to your computer and use it in GitHub Desktop.
Generic where type is constrained by class and protocol
protocol MyProtocol {
func methodFromProtocol()
}
class MyClass {
func methodFromClass() {
println("hello class")
}
}
class MyClassImplementingMyProtocol : MyClass, MyProtocol {
func methodFromProtocol() {
println("hello protocol")
}
}
func doThing<T: MyClass where T: MyProtocol>(myThing: T) {
myThing.methodFromProtocol()
myThing.methodFromClass()
}
doThing(MyClassImplementingMyProtocol())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment