Immutable or not immutable?

后端 未结 6 953
离开以前
离开以前 2020-12-08 23:06

Ok, as I understand it, immutable types are inherently thread safe or so I\'ve read in various places and I think I understand why it is so. If the inner state of a

6条回答
  •  有刺的猬
    2020-12-08 23:24

    I believe that one thing adding to the rather lengthy discussion on this topic is that immutability/mutability should be considered along with scope.

    As an example:

    Say that I am working with a C# project and I define a static class with static data structures, and I define no way to modify those structures in my project. It is in effect a read-only cache of data that I can use, and for the purposes of my program, it is immutable from one run of my program to the next. I have total control over the data, and I/the user am/is unable to modify it at run time.

    Now I modify my data in my source code and re-run the program. The data has changed, so in the highest sense of the word, the data is no longer immutable. From the highest level perspective, the data is actually mutable.

    I would therefore posit that what we need to be discussing is not the black and white question blanketing something as immutable or not, but rather we should consider the degree of mutability of a particular implementation (as there are few things that actually never change, and are therefore truly immutable).

提交回复
热议问题