Skip to content

Instantly share code, notes, and snippets.

@hachinobu
Last active December 22, 2015 05:24
Show Gist options
  • Save hachinobu/f7db9a840ef9d14ae3c8 to your computer and use it in GitHub Desktop.
Save hachinobu/f7db9a840ef9d14ae3c8 to your computer and use it in GitHub Desktop.
Protocol Type generic
protocol MyProtocol {
var myName: String { get }
init()
}
struct MyA: MyProtocol {
var myName: String {
return "My Name is A"
}
}
struct MyB: MyProtocol {
var myName: String {
return "My Name is B"
}
}
func printMy<T: MyProtocol>(my: T.Type) {
let my = T()
print(my.myName)
}
func printInitT<T: MyProtocol>(my: T) {
print(my.myName)
}
printMy(MyA.self)
printMy(MyB.self)
printInitT(MyA())
printInitT(MyB())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment