https://kotlinlang.org logo
#getting-started
Title
# getting-started
l

Landry Norris

03/07/2024, 6:31 PM
Is there a good way to get the KProperty for a delegate when it's initialized? I'm trying to simplify creating variables for a software that binds UI inputs to variables. I have the get/set working binding property.name, but I need to call a method to tell the UI to create the binding. This should ideally be in the constructor.
Right now, my delegate looks like
Copy code
class UIValue(val default: Double) {
    operator fun setValue(ref: Any?, property: KProperty<*>, value: Double) {
        Library.putValue(property.name, value)
    }
    operator fun getValue(ref: Any?, property: KProperty<*>): Double {
        Library.getValue(property.name, default)
    }
}
fun uiValue(default: Double) = UIValue(default)
I need a place to call Library.setDefault(name, value)
For now, I'm calling this in the getter if it hasn't been called yet, and requiring that all values have a dummy getter call at the start, but I don't love that at all.
l

Landry Norris

03/07/2024, 6:58 PM
Thanks. Looks like that works.