https://kotlinlang.org logo
Title
j

Jonathan Hollingsworth

10/07/2021, 3:28 AM
Hey folks, I want to do something like the following
val components: List<Component> = BuilderClass(...some stuff...).into(Component)
I.e. you use the 
into
 function to take any Class and return a list of that class Something like
Class BuilderClass(...) {

  fun into(klass: KClass<Any>): List<what_goes_here?> {
     ...
Anyone know if this is possible?
p

Paul Griffith

10/07/2021, 3:36 AM
fun <T : Any> into(klass: KClass<T>): List<T> {
?
the more Kotlin idiomatic way is
inline fun <reified T : Any> into(): List<T> {
, which would be called something like
into<Component>()
j

Jonathan Hollingsworth

10/07/2021, 3:38 AM
I need to introspect the instance inside
into()
in order to map a returned sql result set onto properties, so I’m guessing I’ll need the class as an argument in the func?
This is pretty cool though, thanks
But will mean I’ve got the rather unweildy syntax of
BuilderClass(...).into<Component>(Component::class)
t

Tobias Suchalla

10/07/2021, 6:00 AM
If you go with the
reified
approach from above, you don't need the explicit class parameter. You can just use
T::class
in your function body since the type is no longer erased.
☝️ 2
j

Jonathan Hollingsworth

10/07/2021, 6:45 AM
Oh, wow. That looks excited. I need to read about
reified
then. Thank you