mardi 9 janvier 2018

Error when use callBy on a function with default parameters in Kotlin

I try to call a function with default parameters values without put parameters in Kotlin.

For example:

class Test {
    fun callMeWithoutParams(value : Double = 0.5) = value * 0.5

    fun callIt(name: String) = this.javaClass.kotlin
            .members.first { it.name == name }
            .callBy(emptyMap())
}

fun main(args: Array<String>) {
   println(Test().callIt("callMeWithoutParams"))
}

I have the exception:

Exception in thread "main" java.lang.IllegalArgumentException: No argument provided for a required parameter: instance of fun 
 Test.callMeWithoutParams(kotlin.Double): kotlin.Double
     at kotlin.reflect.jvm.internal.KCallableImpl.callDefaultMethod(KCallableImpl.kt:139)
    at kotlin.reflect.jvm.internal.KCallableImpl.callBy(KCallableImpl.kt:111)
    at Test.callIt(Main.kt:15)
    at MainKt.main(Main.kt:20)

Strange because the parameter is not required but optional...





Aucun commentaire:

Enregistrer un commentaire