dimanche 30 septembre 2018

Generic function works, but generic class doesn't?

I would like a class that is a generic for KProperty1, I can do this for a function, but not a class:

import kotlin.reflect.KProperty1

data class Dog(val name: String, val age: Int)

fun <P: KProperty1<*, *>> reflectionHelper(input: P) = input.name

class ReflectionHelper<P: KProperty1<*, *>> {
}

fun main(args : Array<String>) {
    println(reflectionHelper(Dog::age))  // Works
    val helper = ReflectionHelper<Dog::age>()  // Error: Type inference failed
}





Aucun commentaire:

Enregistrer un commentaire