https://kotlinlang.org logo
#reflect
Title
# reflect
j

jeggy

08/02/2021, 9:12 PM
Is it possible for me to create a function that would look something like this?
Copy code
interface A { val field: Int }
data class B(override val field: Int): A

fun <T: A> myFunction(property: KProperty1<T, Int>? = null) {
    val kProperty = (property ?: A::field) as KProperty1<Any, Int>
    /* Something */
}

fun main() {
    myFunction(B::field)
}
Solved it with a small hack 👌
e

ephemient

08/02/2021, 10:54 PM
did you try
Copy code
fun myFunction(property: KProperty1<out A, Int>)
?
j

jeggy

08/03/2021, 8:32 AM
That doesn't work
I tried with
<out T, Int>
. But that breaks the usage of
kProperty.get
as now it requires a
Nothing
object instead of
T
or
A
.