Forked from LeeKahSeng/PATs-Dynamic-Dispatch-Generic.swift
Created
June 10, 2020 07:06
-
-
Save liuzhida33/3fdfb190eb1190d8d910dd11fe10918b 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