Using 'Protocol' as a concrete type conforming to protocol 'Protocol' is not supported

extension-methods, protocols, swift

Solution

Fix is replacing `Element: Animal` with `Element == Animal`.

Problem

I have the following swift code: ``` protocol Animal { var name: String { get } } struct Bird: Animal { var name: String var canEat: [Animal] } struct Mammal: Animal { var name: String } extension Array where Element: Animal { func mammalsEatenByBirds() -> [Mammal] { var eatenMammals: [Mammal] = [] self.forEach { animal in if let bird = animal as? Bird { bird.canEat.forEach { eatenAnimal in if let eatenMammal = eatenAnimal as? Mammal { eatenMammals.append(eatenMammal) } else if let eatenBird = eatenAnimal as? Bird { let innerMammals = eatenBird.canEat.mammalsEatenByBirds() eatenMammals.append(contentsOf: innerMammals) } } } } return eatenMammals } } ``` The compiler does not let me compile complaining: Using 'Animal' as a concrete type conforming to protocol 'Animal' is not supported at the point where I recursively call the function mammalsEatenByBirds() I have seen some other answers but could not relate my problem to any of those.

Original source

Related problems