Last active
March 29, 2016 12:47
-
-
Save ktakayama/edbecd61e5bd0c6fbd28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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