How could I get method reference to property setter without using kotlin-reflect?
Basically, if I'll write my code in java way it's super simple
fun setValue(i: Int) = Unit
val a: (Int) -> Unit = this::setValue
But for var value: Int
I'm getting
var value = 1
val a: KMutableProperty0<Int> = this::value
this::value
is a property reference. It returns a KMutableProperty
. To get the setter you will need the setter
field of KMutableProperty
. So you will need this:
class C {
var field: Int = 1
fun getFieldSetter(): (Int) -> Unit{
return this::field.setter
}
}
Slightly shorter: just use this::value::set
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With