Following playground-ready code ;)
protocol Prot {
static func instanceArray() -> [Self]
}
struct A {
init() {
// Do something
}
}
extension A: Prot {
static func instanceArray() -> [A] {
return [A(), A()]
}
}
func work() -> [Prot] {
return A.instanceArray()
}
The compiler will throw an error at return A.instanceArray()
: Cannot convert return expression of type [A] to return type [Prot]
Why is the compiler not able to convert these types even though A
implements the protocol Prot
?
I know that a workaround is to change the return type of instanceArray()
to [Prot]
but I don't really like this solution.
Is this a compiler bug or a feature?