Questions tagged [mutable]

A mutable can be modified after it is created.

A mutable can be modified after it is created. It is a keyword in functional programming opposite to immutable.

Related tags:

1184 questions
939
votes
17 answers

List of lists changes reflected across sublists unexpectedly

I created a list of lists: >>> xs = [[1] * 4] * 3 >>> print(xs) [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then, I changed one of the innermost values: >>> xs[0][0] = 5 >>> print(xs) [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]] Why did every first…
Charles Anderson
  • 19,321
  • 13
  • 57
  • 73
623
votes
18 answers

Does the 'mutable' keyword have any purpose other than allowing a data member to be modified by a const member function?

A while ago, I came across some code that marked a data member of a class with the mutable keyword. As far as I can see it simply allows you to modify a member in a const-qualified member method: class Foo { private: mutable bool done_; …
Rob
  • 76,700
  • 56
  • 158
  • 197
553
votes
16 answers

Why are mutable structs “evil”?

Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What's the actual problem with mutability and structs in C#?
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
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
205
votes
17 answers

Why can't strings be mutable in Java and .NET?

Why is it that they decided to make String immutable in Java and .NET (and some other languages)? Why didn't they make it mutable?
chrissie1
  • 5,014
  • 3
  • 26
  • 26
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
163
votes
14 answers

Existence of mutable named tuple in Python?

Can anyone amend namedtuple or provide an alternative class so that it works for mutable objects? Primarily for readability, I would like something similar to namedtuple that does this: from Camelot import namedgroup Point = namedgroup('Point',…
Alexander
  • 105,104
  • 32
  • 201
  • 196
135
votes
4 answers

What's the difference between placing "mut" before a variable name and after the ":"?

Here are two function signatures I saw in the Rust documentation: fn modify_foo(mut foo: Box) { *foo += 1; *foo } fn modify_foo(foo: &mut i32) { *foo += 1; *foo } Why the different placement of mut? It seems that the first function could also…
Jimmy Lu
  • 4,810
  • 7
  • 25
  • 30
124
votes
5 answers

Create mutable List from array?

I have an array I'd like to turn into a List, in order to modify the contents of the array. Stack Overflow has plenty of questions/answers that address Arrays.asList() and how it only provides a List view of the underlying array, and how attempting…
ericsoco
  • 24,913
  • 29
  • 97
  • 127
114
votes
12 answers

Is Integer Immutable

I know this is probably very stupid, but a lot of places claim that the Integer class in Java is immutable, yet the following code: Integer a=3; Integer b=3; a+=b; System.out.println(a); Executes without any trouble giving the (expected) result 6.…
K.Steff
  • 2,164
  • 3
  • 19
  • 25
112
votes
2 answers

How I can mutate a struct's field from a method?

I want to do this: struct Point { x: i32, y: i32, } impl Point { fn up(&self) { self.y += 1; } } fn main() { let p = Point { x: 0, y: 0 }; p.up(); } But this code throws a compiler error: error[E0594]: cannot…
alxkolm
  • 1,911
  • 2
  • 14
  • 11
102
votes
7 answers

Does Java have mutable types for Integer, Float, Double, Long?

I am in a situation where I want to use mutable versions of things like Integer. Do I have to use these classes (below) or does Java have something built in? http://www.java2s.com/Code/Java/Data-Type/Amutableintwrapper.htm
smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
99
votes
9 answers

How can I emulate a mutable string in Python (like StringBuffer in Java or StringBuilder in C#)?

Since Python's strings are immutable, it's inefficient to edit them repeatedly in loops. How can I use a mutable data structure to implement string operations, so as to avoid making lots of temporary strings?
user2902773
  • 991
  • 1
  • 6
  • 4
93
votes
6 answers

volatile vs. mutable in C++

I have a question about the difference between volatile and mutable. I noticed that both of the two means that it could be changed. What else? Are they the same thing? What's the difference? Where are they applicable? Why the two ideas are proposed?…
skydoor
  • 25,218
  • 52
  • 147
  • 201
92
votes
5 answers

F#: let mutable vs. ref

First, I acknowledge the possibility that this question could be a duplicate; just let me know. I'm curious what the general "best practice" is for those situations when mutability is desired. F# seems to offer two facilities for this: the let…
J Cooper
  • 16,891
  • 12
  • 65
  • 110
1
2 3
78 79