Created
October 24, 2017 02:12
-
-
Save LeeKahSeng/5230b0f0f6f6d57b93499d2b0a7e0bc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
struct AnyAnimal: Animal { | |
let name: String | |
private let walker: () -> Void | |
private let eater: (Any) -> Void | |
init<T: Animal>(_ animal: T) { | |
name = animal.name | |
walker = { | |
animal.walk() | |
} | |
eater = { food in | |
guard let f = food as? T.FoodType else { return } | |
animal.eat(food: f) | |
} | |
} | |
func walk() { | |
walker() | |
} | |
func eat(food: Any) { | |
eater(food) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment