In wwdc2022 apple used static func to create model, which used later in type(of: )..
Here is a video https://developer.apple.com/videos/play/wwdc2022/110352/
I wanna simplify example, what i should do in here?
<code>protocol QAnimalFeed {}
protocol QAnimal {
associatedtype Feed: QAnimalFeed
func eat(_ food: Feed)
}
class Cat {}
extension Cat: QAnimal {
struct Feed: QAnimalFeed {}
func eat(_ food: Feed) {
print("cat eating")
}
}
class Doge {}
extension Doge: QAnimal {
struct Feed: QAnimalFeed {}
func eat(_ food: Feed) {
print("dog eating")
}
}
func main() {
let food: [any QAnimalFeed] = [Doge.Feed(), Cat.Feed()]
let cells: [any QAnimal] = [Cat(), Cat()]
cells.enumerated().forEach { index, animal in
let oneFood = food[index]
feed(animal)
}
func feed(_ animal: some QAnimal) {
// what is here
}
}
</code>
<code>protocol QAnimalFeed {}
protocol QAnimal {
associatedtype Feed: QAnimalFeed
func eat(_ food: Feed)
}
class Cat {}
extension Cat: QAnimal {
struct Feed: QAnimalFeed {}
func eat(_ food: Feed) {
print("cat eating")
}
}
class Doge {}
extension Doge: QAnimal {
struct Feed: QAnimalFeed {}
func eat(_ food: Feed) {
print("dog eating")
}
}
func main() {
let food: [any QAnimalFeed] = [Doge.Feed(), Cat.Feed()]
let cells: [any QAnimal] = [Cat(), Cat()]
cells.enumerated().forEach { index, animal in
let oneFood = food[index]
feed(animal)
}
func feed(_ animal: some QAnimal) {
// what is here
}
}
</code>
protocol QAnimalFeed {}
protocol QAnimal {
associatedtype Feed: QAnimalFeed
func eat(_ food: Feed)
}
class Cat {}
extension Cat: QAnimal {
struct Feed: QAnimalFeed {}
func eat(_ food: Feed) {
print("cat eating")
}
}
class Doge {}
extension Doge: QAnimal {
struct Feed: QAnimalFeed {}
func eat(_ food: Feed) {
print("dog eating")
}
}
func main() {
let food: [any QAnimalFeed] = [Doge.Feed(), Cat.Feed()]
let cells: [any QAnimal] = [Cat(), Cat()]
cells.enumerated().forEach { index, animal in
let oneFood = food[index]
feed(animal)
}
func feed(_ animal: some QAnimal) {
// what is here
}
}
New contributor
Caramel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.