Skip to content

Instantly share code, notes, and snippets.

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 liuzhida33/3fdfb190eb1190d8d910dd11fe10918b to your computer and use it in GitHub Desktop.
Save liuzhida33/3fdfb190eb1190d8d910dd11fe10918b to your computer and use it in GitHub Desktop.
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