Setters called after assignment


#1

I have a class like this:

class A() : B() {

    var text: String? = null
        set(value) {
            update()
        }
}

The update() method determine if text is null and do some stuff. But text == null always returns true at first assignment. I guess that the setter function is called before assignment. How should I do if I want to call a function after assignment?


#2

Your setter function doesn’t update the property value at all. In order to update it, add field = value to the body of your setter.


#3

You probably could be interested in delegating the recurring logic of such properties to a property delegate. Check out observable delegates in the standard library.


#4

That makes sense. I’ve tried text = value before but the setter is called recursively, so I considered that the assignment would be done implicitly…


#5

field is a keyword, instead of field name text