Skip to content

Instantly share code, notes, and snippets.

@ktakayama
Last active March 29, 2016 12:47
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 ktakayama/edbecd61e5bd0c6fbd28 to your computer and use it in GitHub Desktop.
Save ktakayama/edbecd61e5bd0c6fbd28 to your computer and use it in GitHub Desktop.
protocol SpeakProtocol {
func speak() -> String
// func action() -> String
}
extension SpeakProtocol {
func speak() -> String {
print("SpeakProtocol's speak")
return "I am anything"
}
func action() -> String {
print("SpeakProtocol's action")
return speak()
}
}
struct Human: SpeakProtocol {
func speak() -> String {
print("Human's speak")
return "I am human"
}
func action() -> String {
print("Human's action")
return speak()
}
}
struct Devil: SpeakProtocol {
func speak() -> String {
print("Devil's speak")
return "I am devil"
}
}
print("======== human")
let human = Human()
human.speak()
human.action()
print("======== protocol")
let p = Devil()
p.speak()
p.action()
print("======== generic function")
func speakAndAction<T: SpeakProtocol>(speaker: T) -> String {
return (speaker.action())
}
speakAndAction(human)
speakAndAction(p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment