I have some issues with inheritance and generics classes. I want to return an object of a subclass (with an specific generic type) in a function that return an object of the parent class with a generic metatype.
More or less, I have this structure of classes and functions:
class Element {}
class Item:Element {}
class A<aType:Element> { }
class B:A<Item> { }
class SomeClass {
func foo() -> A<Element> {
return A<Element>()
}
}
class OtheClass:SomeClass {
override func foo() -> A<Element> {
return B() // An error where!
}
}
The thing that i can't do is return B()
in OtherClass's foo function. The error is:
Cannot convert return expression of type 'B' to return type 'A< Element >'
Is there any way to do it?
The solution that I have found is:
class CoreElement {}
class CoreItem:CoreElement {}
class CoreA<aType:CoreElement> { }
class CoreB:CoreA<CoreItem> { }
class CoreSomeClass<T:CoreElement> {
func foo() -> CoreA<T> {
return CoreA<T>()
}
}
class CoreOtherClass:CoreSomeClass<CoreItem> {
override func foo() -> CoreA<CoreItem> {
return CoreB()
}
}