如何区分绑定的可调用成员引用和kotlin中相同types的函数?
当涉及方法签名或定义时,是否有任何区分的方法
users().indexOn(User::id)
和
users().indexOn { it.id() }
? 也就是说,要指定绑定成员引用是必需的,而不是函数实例,反之亦然。 以上是上面例子的签名:
fun indexOn(function: (T) -> U): List
在同样的说明,是否有可能区分构造函数引用和返回一个types的函数? 例如, query(::SomeLookup)
与query { SomeLookup(args) }
(如果可能,不包含kotlin.reflect)
在types系统中区分lambda和函数引用的一种方法是使用reflection接口KFunction
,例如:
fun f(ref: T) where T : () -> Unit, T : KFunction { /* ... */ }
fun g() { println("hello") } f(::g) // OK f { println("hello") } // Error: type parameter bound is not satisfied
尽管现在我无法用其他方法来工作。
此外,我发现没有办法区分一个构造函数的types。 但是,在运行时,通过reflection很容易:可以检查KFunction<*>
的KFunction<*>
是否为null。