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

牧云@^-^@ 提交于 2019-12-06 06:00:23

From a bit of testing, a KClass won't keep track of the actual object it was created from, the main difference being that this::class will use the runtime type of this.

You can verify this by querying information about all the parameters:

 name | isOptional | index |     kind |          type
-----------------------------------------------------
 null        false       0   INSTANCE            Test
value         true       1      VALUE   kotlin.Double

The first parameter is actually the instance of the class. Using this::callMeWithoutParams will keep track of this, removing the first row of the table, but naturally doesn't allow for finding the member by name. You can still call the method by providing the object:

fun callIt(name: String) { 
    val member = this::class.members.first { it.name == name }
    member.callBy(mapOf(member.instanceParameter!! to this))
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!