Consider the following in Swift:
struct GenericStruct<T> {}
class A {}
class B: A {}
func doSomething() -> GenericStruct<A> {
return GenericStruct<B>()
}
This gives the error:
Cannot convert return expression of type
GenericStruct<B>
to return typeGenericStruct<A>
But B
is a subclass of A
.
- Why can't Swift convert
GenericStruct<B>
toGenericStruct<A>
? - What should be done in this scenario?