I have an abstract class, let's call it A.
abstract class A(private val name: String) {
fun read(key: String): Entity {
...
}
fun write(entity: Entity) {
...
}
abstract val mapper: Mapper<Any>
...
interface Mapper<T> {
fun toEntity(entry: T): Entity
fun fromEntity(entity: Entity): T
}
...
It has an abstract mapper. The point of that is that I can map different objects to Entity and use read and write.
My child class, let's call it B, is structured like this:
class B(private val name: String) : A(name) {
override val mapper = AnimalMapper
object AnimalMapper: Mapper<Animal> {
override fun fromEntity(entity: Entity): Animal {
TODO("not implemented")
}
override fun toEntity(animal: Animal): Entity {
TODO("not implemented")
}
}
}
Ideally, I wanted to have an interface in the Mapper generic and not Any, but I'm simplifying this just for the question.
The issue is that I get this error:
Type of 'mapper' is not a subtype of the overridden property 'public abstract val mapper: Mapper<Any> defined in ...
Why is that?
Asays that users can map anything. But if the implementation isB, they can only map animals. That raises red flags.