Cannot reference companion methods

I have the following code:

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
}

When I try to call process(::A.bb), I get the following error:

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

Is there a way to refer to methods of companion objects?

+4
source share
1 answer

It will be syntactically A.Factory:bb, but it will not work. First bbis A.Factory.() -> Int, while required () -> Int.

Secondly, called links to object members are not currently supported, as the Kotlin compiler says. Here's the parent task for all member tasks called: https://youtrack.jetbrains.com/issue/KT-1183 .

+3
source

Source: https://habr.com/ru/post/1628509/


All Articles