Setters called after assignment


I have a class like this:

class A() : B() {

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

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?


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.


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.


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…


field is a keyword, instead of field name text