Answer the question
In order to leave comments, you need to log in
Why is the copy function constantly used in reactive approaches?
Very often on this forum I see recommendations regarding the processing of responses in Rx chains, where they write that it is necessary to minimize state changes and for this it is necessary to use the kotlin copy function.
Now I am in the process of learning and have not yet written complex applications. Can you explain with some simple example what problems can arise if this is not done?
I don't understand the advantages of using copy over directly accessing setters and setting values through them.
Answer the question
In order to leave comments, you need to log in
The advantage is that when using immutable objects, the data flow remains unidirectional. And if you mutate objects, hellish special effects can happen. For example.
data class Data(var x : Int)
Observable.just(1, 1, 2, 2)
.doOnNext { print("I'm $it") }
.map(::Data)
. distinctUntilChanged()
.map{
it.apply{ x++ }
}
.doOnNext { print("Now I'm $it") }
.subscribe()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question