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:

1042 questions
754
votes
17 answers

List of lists changes reflected across sublists unexpectedly

I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of the innermost values: myList[0][0] = 5 Now my list…
Charles Anderson
  • 16,347
  • 13
  • 52
  • 71
556
votes
18 answers

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

A while ago I came across some code that marked a member variable of a class with the mutable keyword. As far as I can see it simply allows you to modify a variable in a const method: class Foo { private: mutable bool done_; public: …
Rob
  • 70,036
  • 53
  • 151
  • 189
513
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
  • 161,833
  • 52
  • 243
  • 303
198
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
  • 4,884
  • 3
  • 23
  • 24
195
votes
17 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,121
  • 3
  • 12
  • 10
181
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
  • 56,458
  • 39
  • 130
  • 203
133
votes
11 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
  • 87,529
  • 23
  • 162
  • 169
109
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,012
  • 3
  • 19
  • 24
92
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
  • 20,453
  • 20
  • 89
  • 117
90
votes
7 answers

Good uses for mutable function argument default values?

It is a common mistake in Python to set a mutable object as the default value of an argument in a function. Here's an example taken from this excellent write-up by David Goodger: >>> def bad_append(new_item, a_list=[]): …
Jonathan
  • 84,911
  • 94
  • 244
  • 345
90
votes
7 answers

Immutable/Mutable Collections in Swift

I was referring to Apple's Swift programming guide for understanding creation of Mutable/ immutable objects(Array, Dictionary, Sets, Data) in Swift language. But I could't understand how to create a immutable collections in Swift. I would like to…
Pranav Jaiswal
  • 3,672
  • 3
  • 28
  • 50
89
votes
3 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
  • 3,990
  • 5
  • 22
  • 29
87
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,432
  • 11
  • 58
  • 104
86
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
  • 23,142
  • 48
  • 138
  • 193
85
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
  • 8,687
  • 18
  • 64
  • 104
1
2 3
69 70