Yogeshvu
08/14/2024, 8:19 PMdataStructureClass
as a function argument? for instance would like to implement something like this.. but not sure how?
///dataStructureClass --> is the target class to be passed as an argument
fun test(dataStructureClass: Class<*>){
val myTest = listOf<dataStructureClass>()
val testVariables = listOf<Int>()
}
Klitos Kyriacou
08/14/2024, 9:21 PMClass
(or KClass
, the Kotlin equivalent of Java's Class
) as a generic type parameter. You can't. Function arguments of type KClass
are actual objects that exist at runtime, while generic type parameters are not tangible objects, but just class names that tell the compiler something about a type. What you can do instead is use generics:
fun <T: Any> test(dataStructureClass: KClass<T>) {
val myTest = listOf<T>()
}
But how do you intend to use this myTest
object once you've created it?Yogeshvu
08/14/2024, 11:13 PM