Post

Replies

Boosts

Views

Activity

Reply to Having problems trying out the example code in this session
import Cocoa protocol AnimalFeed {  static func grow() -> Self } protocol Animal {  associatedtype Feed: AnimalFeed  func eat(_ food: Feed) } struct Hay: AnimalFeed {  static func grow() -> Self {   print("growing hay")   return Hay()  } } struct Pellet: AnimalFeed {  static func grow() -> Self {   print("growing pellets")   return Pellet()  } } struct Horse: Animal {  func eat(_ food: Hay) {   print("horse eating \(food)")  } } struct Dog: Animal {  func eat(_ food: Pellet) {   print("dog eating \(food)")  } } struct Farm {  func feed(_ animal: some Animal) {   let food = type(of: animal).Feed.grow()   animal.eat(food)  }  func feedAll(_ animals: [any Animal]) {   for animal in animals {    feed(animal)   }  } } let animals: [any Animal] = [  Horse(),  Horse(),  Dog(), ] let farm = Farm() farm.feedAll(animals) according to the video, that should compile... aside: I opened the forum post creator from the Embrace Swift Generics video, which added the tag for the video, so I assumed it would signal which video I was talking about, but I guess the forum is not very useful in that regard
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’22