a newbie question: Kotlin reflection, if I have a ...
# getting-started
s
a newbie question: Kotlin reflection, if I have a
p: KProperty<SomeClass, *>
, how do I check this property is defined as val or var in it's containing class?
w
You can check if it's instance of
KMutableProperty
Copy code
class A(var a: Int)

class B(val a: Int)

fun isMutable(prop: KProperty<*>): Boolean = prop is KMutableProperty<*>

println(isMutable(A(1)::a)) // true
println(isMutable(B(1)::a)) // false
1
Adjusted to your
SomeClass
type:
Copy code
interface SomeClass {
    val a: Int
}

class A(override var a: Int): SomeClass
class B(override val a: Int): SomeClass

fun isMutable(prop: KProperty1<out SomeClass, *>): Boolean = prop is KMutableProperty<*>

println(isMutable(A::a))
println(isMutable(B::a))
s
thank you!!!
👍 1