Note : For the sake of simplicity, I removed methods like init and specific functions.
I have an abstract class called Role.
abstract class Role {}
From that class I have inherited many classes like Knight, Sorcerer ... etc.
class Knight(context: Context) : Role() {}
class Srocerer(context: Context) : Role() {}
And Then I have created another abstract Class having a generic parameter R : Role
abstract class Turn<R : Role> {}
Not every role (player) can play a turn
And as you may expect I also inherited some classes like KngithTurn, SorcererTurn ... etc.
class KnightTurn(role : Knight) : Turn<Knight>() {}
class SorcererTurn(role : Sorcerer) : Turn<Sorcerer>() {}
The problem is when I create an arrayList of Turn, and try to add an object of type KnightTurn or SorcererTurn, the IDE says that there is a type mismatch despite the fact that they are inherited from class Turn.
var list = ArrayList<Turn<Role>>()
val knight = KnightTurn(Knight(baseContext))
list.add(knight)
// Type mismatch
// Required:Turn<Role>
// Found:KnightTurn
In java, I just solve the problem like this:
ArrayList<Turn<Role?>>
How can I do it in Kotlin, or is there any other solution? Thank you in advance.