guppyfaced
02/01/2019, 11:40 AMdata class
using parameters? I'm thinking of doing it with a map of values, but if there's a constructor method I could call, that would be great.diesieben07
02/01/2019, 11:42 AMguppyfaced
02/01/2019, 12:22 PMdiesieben07
02/01/2019, 12:25 PMdata class Person(val name: String, val age: Int)
fun main() {
val args = mapOf("name" to "Peter", "age" to 23)
val constructor = Person::class.primaryConstructor!!
val person = constructor.callBy(
constructor.parameters.associateWith { args[it.name] }
)
println(person)
}
guppyfaced
02/01/2019, 12:40 PMdiesieben07
02/01/2019, 12:41 PMguppyfaced
02/01/2019, 12:42 PMdiesieben07
02/01/2019, 12:43 PMkotlin-reflect
on the classpath (compile 'org.jetbrains.kotlin:kotlin-reflect'
when using Gradle).guppyfaced
02/01/2019, 12:46 PMconstructor.parameters.associateWith { args[it.name]
diesieben07
02/01/2019, 12:50 PMassociate { it to args[it.name] }
for 1.2.x insteadguppyfaced
02/01/2019, 12:54 PMsnowe
02/01/2019, 8:09 PMimplementation(kotlin("reflect"))