有没有可能在Kotlin中创建对伴侣对象generics方法的引用?

是否有可能将一个variables的值设置为一个方法,其中该方法是一个Companion对象,并有一个types参数? 像下面这样:

class A { companion object B { fun foo(n: T) { } } } val b = AB::foo 

不,在types系统中没有generics函数引用的表示。 例如,它没有量化的types,它可能代表你正在尝试引用的forms如forall T . (T) -> Unit的函数forall T . (T) -> Unit forall T . (T) -> Unit

您只能对generics函数进行非generics引用,为此您必须提供期望types的具体types(从引用被分配或传递的地方获取),例如,这将起作用:

 class A { object B { fun  foo(n: T) { } } } val b: (Int) -> Unit = AB::foo // substitutes T := Int val c: (String) -> Unit = AB::foo // T := String here fun f(g: (Double) -> Unit) = println(g(1.0)) f(AB::foo) // also allowed, T := Double inferred from the expected type