When I iterate over a list of Sprites and check whether they implement UsesObjectPool or not, I am encountering these problems due to type erasure:
*If I try to check its type I get:
Cannot check for instance of erased type: UsesObjectPool
if(it is UsesObjectPool<Sprite>)
{
it.objectPool.releaseObject(it)
}
- If I use the wildcard * to check, it says that:
"required parameter Nothing, found Sprite & UsesObjectPool:
if(it is UsesObjectPool<*>)
{
it.objectPool.releaseObject(it)
}
*If I cast the it to UsesObjectPool, I get:
Type argument is not within its bounds. Expected: UsesObjectPool Found: Sprite
if(it is UsesObjectPool<*>)
{
(it as UsesObjectPool<Sprite>).objectPool.releaseObject(it)
}
The generic class and interface:
interface UsesObjectPool<T> where T :UsesObjectPool<T>, T : Sprite
{
val objectPool: BaseObjectPool<T>
}
abstract class BaseObjectPool<T> where T : UsesObjectPool<T>, T : Sprite
{
fun releaseObject(instance: T)
{
//Implementation
}
}