is it possible to get an instance for a `KClass` o...
# reflect
m
is it possible to get an instance for a
KClass
of lambda and for a
KClass
of anonymous class? In the following example, I can't get an instance through
createInstance()
on 2 or 3:
Copy code
fun interface A<Input, Output> {
    fun action(value: Input): Output
}
// 1
class B : A<Float, Int> {
    override fun action(value: Float): Int {
        return value.toInt()
    }
}
println(B::class.createInstance()) // ok, no problems here

// 2
class BAnonymous = object : A<Float, Int> {
    override fun action(value: Float): Int {
        return value.toInt()
    }
}
println(BAnonymous::class.createInstance()) // java.lang.IllegalArgumentException: Class should have a single no-arg constructor

// 3
val BLambda = A<Float, Int> { it.toInt() }
println(BLambda::class.createInstance()) // kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unresolved class: class com.example.MainKt$$Lambda$15/0x0000000800ba5440
I need to call
action()
on each one of those, and either using
Copy code
kclass.declaredFunctions.single().call(instance, /*Float number, e.g 2.3f*/)
requires an instance for the first parameter of
call
or even creating an instance with
createInstance()
and calling
action
on it directly crashes with the aforementioned errors