The full sample code is as follows:
protocol Animal {
func greet(_ animal: some Animal)
func _greetedByDog(_ dog: Dog)
}
extension Animal {
func greet<A: Animal>(_ animal: A) {
print("\(Self.self) sniffs \(A.self) cautiously")
}
func _greetedByDog(_ dog: Dog) {
print("\(Dog.self) sniffs \(Self.self) cautiously")
}
}
struct Cat: Animal {}
struct Dog: Animal {
func greet(_ animal: some Animal) {
animal._greetedByDog(self)
}
func _greetedByDog(_ dog: Dog) {
print("Bark in excitement")
}
}
func walk<each A: Animal>(_ animals: repeat each A, towards animal: some Animal) {
repeat (each animals).greet(animal)
}
If we do the same test as last time:
walk(Cat(), Dog(), towards: Dog())
We get:
Cat sniffs Dog cautiously
Bark in excitement