The following code compiled without issue
protocol Animal {
}
var animals = [Animal]()
However, we have a new requirement, where we need to compare array of Animal
protocol Animal {
}
func x(a0: [Animal], a1: [Animal]) -> Bool {
return a0 == a1
}
var animals = [Animal]()
The above code will yield compilation error
protocol 'Animal' as a type cannot conform to 'Equatable'
We tend to fix by
protocol Animal: Equatable {
}
func x(a0: [Animal], a1: [Animal]) -> Bool {
return a0 == a1
}
var animals = [Animal]()
At array declaration line, we are getting error
protocol 'Animal' can only be used as a generic constraint because it has Self or associated type requirements
May I know,
- Why we can have an array of protocol, before the protocol is conforming to Equatable?
- Why we are not allowed to have an array of protocol, once the protocol is conforming to Equatable?
- What are some good ways to fix such error?