How to determine the values ​​changed in the Kotlin data class?

I want to detect any values ​​changed in a property of my class so that I can then perform another operation. In other words, if one of the specific property data changes, then a certain event will be triggered by this time. Actually, if this is a regular class in other programming languages ​​such as Java, then I think I could use setter to do this job after the data has been changed or used by a delegate in C #. But since Kotlin is very new, I did not find any solution at all. I tried to overload the property, but was not successful in any way. I also wanted to use the interface for this, but again, since this is a data class, I had no idea how to do this.

Below is a sample class. In such a situation, how to determine when age or name changes?

data class Person(var Name: String, var Age: Int) 

So, please, if anyone has any ideas about this, please help.

Note. In my situation, a data class should be used.

+4
source share
2 answers

Data classes are not really created for this. Since their properties must be declared in the main constructor, you cannot add custom behavior to them.

However, if you need to, you can achieve this by duplicating properties, and then either using custom setters, or Delegates.observable.

, name age, , :

data class Person(private var _name: String, private var _age: Int) {

    var name = _name
        set(value) {
            println("Name changed from $name to $value")
            field = value // sets the backing field for `name`
            _name = value // sets the `_name` property declared in the primary ctor
        }

    var age = _age
        set(value) {
            println("Age changed from $age to $value")
            field = value
            _age = value
        }

}

, Delegates.observable, , - , , :

data class Person(private var _name: String, private var _age: Int) {

    var name: String by Delegates.observable(_name) { prop, old, new ->
        println("Name changed from $old to $new")
        _name = new
    }

    var age: Int by Delegates.observable(_age) { prop, old, new ->
        println("Age changed from $old to $new")
        _age = new
    }

}

(toString ):

val sally = Person("Sally", 50)
println(sally)      // Person(_name=Sally, _age=50)
sally.age = 51      // Age changed from 50 to 51
println(sally)      // Person(_name=Sally, _age=51)
println(sally.name) // Sally
println(sally.age)  // 51

, :

, , :

class Person(name: String, age: Int) {

    var name: String by Delegates.observable(name) { _, old, new ->
        println("Name changed from $old to $new")
    }

    var age: Int by Delegates.observable(age) { _, old, new ->
        println("Age changed from $old to $new")
    }

}

, , , , . , - ( val var). constructors, properties .

+7

, , , , , .

: , : , , . ( ) , Kotlin :

class Example {
    var p: String by Delegate()
}

: val/var: by. , get() ( set()), getValue() setValue() . - , getValue() ( setValue() - ). :

class Delegate {
    operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
        return "$thisRef, thank you for delegating '${property.name}' to me!"
    }

    operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
        println("$value has been assigned to '${property.name} in $thisRef.'")
    }
}
0

Source: https://habr.com/ru/post/1678925/


All Articles