有一个Kotlin对象列表(这里是ModelTrap
),它们使用各种数据模型进行参数化。我需要能够循环遍历它们并使用与为此checkTraps
方法提供的模型具有相同类型参数化的那些。
//val trapList: MutableList <ModelTrap <*>> = mutableListOf()
inline fun <reified T> checkTraps(model : T) {
for (trap in trapList) {
if (trap is ModelTrap <T>) { // "Cannot check for instance of erased type."
trap.catch(model)
}
}
}
...但是我一直试图弄清楚如何做到这一点。有没有更好的方法来定义该列表?有没有办法检查类型T
的实例?