Miguel Vargas
12/17/2021, 10:33 PMfun assertListTypes(list: List<ParentType>, types: List<KType>) {
assertEquals(list.size, types.size)
types.forEachIndexed { idx, type ->
assert(list[idx] is type)
}
}
assertListTypes(list, listOf(typeOf<Class1>(), typeOf<Class2>()))
But the compiler didn’t like the is
check on the KType. Any ideas?mkrussel
12/17/2021, 10:36 PMis
checks can only be done with compile time constants on the right hand side.
Instead you have to use reflection functions to determine if it is that type.mkrussel
12/17/2021, 10:38 PMKClass.isInstance
could do that but I don't see anything on KType
.Miguel Vargas
12/17/2021, 10:40 PMMiguel Vargas
12/17/2021, 10:48 PMMiguel Vargas
12/17/2021, 10:49 PMfun assertListTypes(list: List<ParentType>, types: List<KClass<*>>) {
assertEquals(list.size, types.size)
types.forEachIndexed { idx, type ->
assert(type.isInstance(list[idx]))
}
}
assertListTypes(list, listOf(Class1::class, Class2::class))
Miguel Vargas
12/17/2021, 10:49 PMDaniel
12/19/2021, 2:36 PM