首页 > 解决方案 > 反射性地引用重载的顶级 Kotlin 函数

问题描述

简而言之,如何在 Kotlin 中对重载的顶级函数进行反射性引用/迭代,例如kotlin.io.println


鉴于以下情况:

object Bar {
    fun foo(x: Int) = Unit
    fun foo(x: Byte) = Unit
    fun foo(x: Float) = Unit
}

我可以通过以下方式迭代各种重载foo

fun main() {
    Bar::class.memberFunctions
        .filter { kFunction -> kFunction.name == "foo" }
        .forEach { kFunction -> println(kFunction) }
}

产生:

fun com.example.Bar.foo(kotlin.Byte): kotlin.Unit
fun com.example.Bar.foo(kotlin.Float): kotlin.Unit
fun com.example.Bar.foo(kotlin.Int): kotlin.Unit

但是foo,如果在顶层(在类或对象定义之外)定义了各种重载,例如:

fun foo(x: Int) = Unit
fun foo(x: Byte) = Unit
fun foo(x: Float) = Unit

然后似乎没有办法引用它们。

我尝试在我的示例(例如main)中使用顶级函数来访问合成类:

::main::class.memberFunctions
    .filter { kFunction -> kFunction.name == "foo" }
    .forEach { kFunction -> println(kFunction) }

但它呕吐的事实是它是合成的:

Exception in thread "main" java.lang.UnsupportedOperationException: This class is an internal synthetic class generated by the Kotlin compiler, such as an anonymous class for a lambda, a SAM wrapper, a callable reference, etc. It's not a Kotlin class or interface, so the reflection library has no idea what declarations does it have. Please use Java reflection to inspect this class.

如何在 Kotlin 中引用顶级重载函数?

更具体地说,在其他包/模块中定义的顶级重载函数,例如kotlin.io.println

标签: functionkotlinreflectionoverloading

解决方案


根据定义,顶级函数没有声明类。

::println.javaClass.declaringClass //will return null

所以你没有一个类可以使用反射,因此,你不能枚举一个包的顶级成员。(如果你愿意交换你的灵魂,可以做一些魔法

引用模棱两可的顶级函数的唯一方法是帮助编译器解决歧义,如下所示:

val functionReference: (Int)->Unit = :foo

然后你可以调用 functionReference()


推荐阅读