function - 反射性地引用重载的顶级 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
?
解决方案
根据定义,顶级函数没有声明类。
::println.javaClass.declaringClass //will return null
所以你没有一个类可以使用反射,因此,你不能枚举一个包的顶级成员。(如果你愿意交换你的灵魂,可以做一些魔法)
引用模棱两可的顶级函数的唯一方法是帮助编译器解决歧义,如下所示:
val functionReference: (Int)->Unit = :foo
然后你可以调用 functionReference()
推荐阅读
- php - 如何从 docker-compose 连接到主机
- rest - 从 REST 和 Websocket 同步数据
- python - 是否可以在 Python 的函数调用中再次使用自增变量
- c++ - 如何将 const char* 作为参数传递给 LPCSTR 参数?
- node.js - 使用 util.promisify 和 exec 不能解决 git hook 中的承诺
- sql - 在 MY SQL 中工作,但不在 MS SQL Server 或 ORACLE 中工作
- python - 如何在使用 get 方法而不是 post 方法的网站中使用 python 登录?
- jwt - 如何使用 JWT 刷新令牌?
- android - App Inventor 是否直接编译为 Java?
- node.js - 实际上我正在尝试从 Nestjs 测试一个函数并收到该错误: