Cant instanciate class with captured val via reflection

This code produce runtime err “IllegalArgumentException: Callable expects 1 arguments, but 0 were provided”:

 fun main(){
    val name = "slon"
    class Slon{
          override val toString = name
    }
    Slon::class.primaryConstuctor!!.call()
}

Could you provide more clear err msg or fix this err?

the call method has a “varargs” parameter, so you need to pass at least a parameter.

passing a null parameter should solve the issue:
Slon::class.primaryConstuctor!!.call(null)

I think the error message is clear, because the call method is defined in KCallable class .

btw, the following line in your code doesn’t seem correct: override val toString = name

shouldn’t it be override fun toString() = name ?

1 Like