我想要一个通用的类KProperty1
,我可以为一个函数做这个,但不是一个类:
import kotlin.reflect.KProperty1
data class Dog(val name: String, val age: Int)
fun <P: KProperty1<*, *>> reflectionHelper(input: P) = input.name
fun <P: KProperty1<*, *>> getReflectionHelper(clazz: P) = ReflectionHelper<P>()
class ReflectionHelper<P: KProperty1<*, *>> {
}
fun main(args : Array<String>) {
println(reflectionHelper(Dog::age)) // Works
val helper1 = getReflectionHelper(Dog::age) // Also Works
val helper2 = ReflectionHelper<Dog::age>() // Error: Type inference failed
}