无法引用随播对象方法
我有以下代码:
fun process(call: () -> Int) { } fun aa() = 5 class A { companion object Factory { fun bb() = 6 } } fun test() { process(::aa) // OK process(::A.bb) // Overload resolution ambiguity }
当我尝试调用process(::A.bb)
,出现以下错误:
Error:Overload resolution ambiguity: public constructor A() defined in ru.netimen.hitch_hikingstats.A public companion object Factory defined in ru.netimen.hitch_hikingstats.A
有什么方法可以引用伴侣对象方法吗?
从句法上来说,它是A.Factory:bb
但它不起作用。 首先, bb
是A.Factory.() -> Int
while () -> Int
是必需的。
其次,像Kotlin编译器说的那样,目前不支持对对象成员的可调用引用。 以下是所有可调用成员任务的父任务: https : //youtrack.jetbrains.com/issue/KT-1183 。