import kotlin.reflect.* import kotlin.reflect.full.* inline fun T.update(other: Any): T { val copy = T::class.memberFunctions .filter { function -> function.name == "copy" && function.parameters.firstOrNull()?.kind == KParameter.Kind.INSTANCE && function.parameters.drop(1).all { it.isOptional } } .maxByOrNull { it.parameters.size } ?: throw NoSuchPropertyException() val args = mutableMapOf(copy.parameters.first() to this) for (function in other::class.memberFunctions) { if ( !function.name.startsWith("component") || function.parameters.singleOrNull()?.kind != KParameter.Kind.INSTANCE ) continue val index = function.name.removePrefix("component").toIntOrNull() ?.takeIf { it > 0 } ?: continue args[copy.parameters[index]] = function.call(other) } return copy.callBy(args) as T } data class A(val name: String, val surname: String) fun main() { println(A("", "").update("Hello" to "world!")) // A(name = "Hello", surname = "world!") }