If I have two `var`s with the exact same implement...
# announcements
f
If I have two `var`s with the exact same implementation in their setter functions, is it possible to extract that logic out into a single function then assign it as the setter?
Yeah, I’m wanting it to include a backing field.
That makes sense. I was kinda hoping for something along the lines of
set(value) = checkValue(field, value)
but this seems clean enough. Thanks
s
What about writing a property delegate?
3
Copy code
class GreatestDelegate<T: Comparable<T>>(initialValue: T) {
    private var backingField: T = initialValue

    operator fun getValue(host: Any?, property: KProperty<*>): T  {
        return backingField
    }

    operator fun setValue(host: Any?, property: KProperty<*>, value: T) {
        if (value > backingField) {
            backingField = value
        }
    }
}

class Test {
    var a: String by GreatestDelegate("")
    var b: String by GreatestDelegate("_")
}
👍 2
f
Excellent, this is more or less the idea I was looking for. Appreciate it!