Questions tagged [immutability]

Immutability is the inability to modify data after it has been created. Modifications are instead made by copying the data. A property of immutable data is that it is *referentially transparent*.

Overview

Immutability is the inability to modify a variable after it is has been created.

It is a pattern found in many branches of programming; immutable objects are used widely within object oriented languages (such as Python's str type, Java's String and Integer type, .NET's System.String, etc.), functional programming (esp. Haskell and other pure languages), and other paradigms.

See also

3670 questions
211
votes
18 answers

Enums in Javascript with ES6

I'm rebuilding an old Java project in Javascript, and realized that there's no good way to do enums in JS. The best I can come up with is: const Colors = { RED: Symbol("red"), BLUE: Symbol("blue"), GREEN:…
cypherfunc
  • 2,251
  • 2
  • 12
  • 14
208
votes
8 answers

Why can tuples contain mutable items?

If a tuple is immutable then why can it contain mutable items? It is seemingly a contradiction that when a mutable item such as a list does get modified, the tuple it belongs to maintains being immutable.
qazwsx
  • 25,536
  • 30
  • 72
  • 106
208
votes
18 answers

Immutable vs Mutable types

I'm confused on what an immutable type is. I know the float object is considered to be immutable, with this type of example from my book: class RoundFloat(float): def __new__(cls, val): return float.__new__(cls, round(val, 2)) Is this…
user1027217
  • 2,251
  • 3
  • 14
  • 13
201
votes
13 answers

Why .NET String is immutable?

As we all know, String is immutable. What are the reasons for String being immutable and the introduction of StringBuilder class as mutable?
Nirajan Singh
  • 2,865
  • 3
  • 25
  • 24
200
votes
12 answers

Mutable vs immutable objects

I'm trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I'm having trouble understanding what the negative impacts are of this. What are the…
Alex Angas
  • 59,219
  • 41
  • 137
  • 210
196
votes
17 answers

Remove a property in an object immutably

I am using Redux. In my reducer I'm trying to remove a property from an object like this: const state = { a: '1', b: '2', c: { x: '42', y: '43' }, } And I want to have something like this without having to mutate the…
Vincent Taing
  • 3,283
  • 2
  • 18
  • 24
188
votes
8 answers

Error: "Cannot modify the return value" c#

I'm using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error Message: Cannot modify the return value of…
P a u l
  • 7,805
  • 15
  • 59
  • 92
176
votes
15 answers

Immutable array in Java

Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn't actually prevent one from doing something like final int[] array = new int[] {0, 1, 2, 3}; array[0] = 42; I want the elements of the array to…
ignoramus
174
votes
13 answers

How to avoid "too many parameters" problem in API design?

I have this API function: public ResultEnum DoSomeAction(string a, string b, DateTime c, OtherEnum d, string e, string f, out Guid code) I don't like it. Because parameter order becomes unnecessarily significant. It becomes harder to add new…
Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
174
votes
7 answers

Swift make method parameter mutable?

How can I deal with this error without creating additional variable? func reduceToZero(x:Int) -> Int { while (x != 0) { x = x-1 // ERROR: cannot assign to 'let' value 'x' } return x } I don't want to create…
Gabriel
  • 1,877
  • 2
  • 12
  • 8
153
votes
10 answers

Advantages of stateless programming?

I've recently been learning about functional programming (specifically Haskell, but I've gone through tutorials on Lisp and Erlang as well). While I found the concepts very enlightening, I still don't see the practical side of the "no side effects"…
Sasha Chedygov
  • 127,549
  • 26
  • 102
  • 115
137
votes
7 answers

What is the difference between immutable and const variables in Rust?

I learned that if a variable is not explicitly declared mutable using mut, it becomes immutable (it cannot be changed after declaration). Then why do we have the const keyword in Rust? Aren't they the same? If not, how do they differ?
7_R3X
  • 3,904
  • 4
  • 25
  • 43
136
votes
13 answers

Why does C# disallow readonly local variables?

Having a friendly debate with a co-worker about this. We have some thoughts about this, but wondering what the SO crowd thinks about this?
Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
136
votes
10 answers

Remove value from object without mutation

What's a good and short way to remove a value from an object at a specific key without mutating the original object? I'd like to do something like: let o = {firstname: 'Jane', lastname: 'Doe'}; let o2 = doSomething(o,…
amann
  • 5,449
  • 4
  • 38
  • 46
134
votes
3 answers

How do I work around mutability in moment.js?

I've run into a problem where I have to store the initial values of a moment object but I'm having some trouble preventing my variable from changing along with the original object. Unfortunately Object.freeze() doesn't work, because moment.js…
Shengbo1618
  • 1,443
  • 2
  • 9
  • 5