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 = { |
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
protocol Animal { | |
var name: String { get } | |
func walk() | |
associatedtype FoodType | |
func eat(food: FoodType) | |
} | |
class Cow: Animal { |
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
// Create concrete type Animal | |
let myTiger = Tiger(withName: "My Tiger") | |
let myCow = Cow(withName: "My Cow") | |
// Instantiate AnyAnimal with concrete type Animal | |
let anyTiger = AnyAnimal.tiger(myTiger) | |
let anyCow = AnyAnimal.cow(myCow) | |
let animalArray: [AnyAnimal] = [anyTiger, anyCow] | |
let foodArray: [Any] = [Meat(), Grass()] |
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
enum AnyAnimal: Animal { | |
case cow(Cow) | |
case tiger(Tiger) | |
var name: String { | |
switch self { | |
case let .cow(animal): | |
return animal.name | |
case let .tiger(animal): |
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
let animalArray: [Animal] = [myTiger, myCow] | |
let foodArray: [Any] = [Meat(), Grass()] | |
for (animal, food) in zip(animalArray, foodArray) { | |
// Feed the animal with their favorite food | |
animal.eat(food: food) | |
} |
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
protocol Animal { | |
var name: String { get } | |
func walk() | |
associatedtype FoodType | |
func eat(food: FoodType) | |
} | |
class Cow: Animal { |
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
protocol Animal { | |
var name: String { get } | |
func walk() | |
} | |
class Cow: Animal { | |
let name: String | |
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
protocol Animal { | |
associatedtype FoodType | |
func eat(food: FoodType) | |
} |
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
protocol Flyable { | |
/// Limit the speed of flyable | |
var speedLimit: Int { get } | |
func fly() | |
} | |
extension Flyable { | |
func fly() { |
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
protocol Flyable { | |
// Make speedLimit only gettable | |
var speedLimit: Int { get } | |
func fly() | |
} | |
class Bird: Flyable { | |
// Make speedLimit private | |
private(set) var speedLimit = 20 |