how to pass a class eg: `dataStructureClass` as a ...
# getting-started
y
how to pass a class eg:
dataStructureClass
as a function argument? for instance would like to implement something like this.. but not sure how?
Copy code
///dataStructureClass --> is the target class to be passed as an argument
fun test(dataStructureClass: Class<*>){
    val myTest = listOf<dataStructureClass>()
    val testVariables = listOf<Int>()
}
k
Your example already passes a class as a function argument. I think what you're asking is how to use an instance of type
Class
(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:
Copy code
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?
👍 1
y
Thanks