concept mutable state in category kotlin

This is an excerpt from Manning's book The Joy of Kotlin.
Applying safe principles like immutability and referential transparency protects programs against accidental sharing of a mutable state, which is a huge source of bugs in multithreaded environments.
In the previous chapters, you learned many techniques to help you write safer programs. Most of these techniques come from functional programming. One of these techniques consist in using immutable data to avoid state mutations. Programs without mutable states are safer, more reliable, and easier to design and scale.
You learned how mutable states can be handled in a functional way by passing the state along as an argument to functions. You saw several examples of this technique. You learned to generate streams of data by passing the state of a generator together with each new value. (If you don’t remember this, review exercise 9.29 where you implemented the
unfold
function by passing along each generated value together with the new state of the generator.) In chapter 12, you also learned how to pass the console as a parameter to send output to the screen and receive input from the keyboard. These techniques can be widely applied to many domains. But this is often understood to mean that functional programming techniques help to safely share mutable state. This is completely wrong.Using immutable data structures, for example, doesn’t help with sharing mutable state. It prevents unintentional sharing of a mutable state by removing state mutation. Passing the state as part of a function parameter and returning a new (immutable) state as part of the result (in the form of a pair containing both the result and the new state) is perfectly fine when dealing with a single thread. But as long as you need to share state mutation between threads, which is pretty much always the case in modern applications, immutable data structures don’t help. To share this kind of data, one needs a mutable reference to it so that the new immutable data can replace the previous one.