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
27
votes
4 answers

Cannot obtain a mutable reference when iterating a recursive structure: cannot borrow as mutable more than once at a time

I'm trying to navigate a recursive data structure iteratively in order to insert elements at a certain position. To my limited understanding, this means taking a mutable reference to the root of the structure and successively replacing it by a…
Fabian Knorr
  • 3,134
  • 3
  • 20
  • 32
27
votes
8 answers

Why the "mutable default argument fix" syntax is so ugly, asks python newbie

Now following my series of "python newbie questions" and based on another question. Prerogative Go to http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables and scroll down to "Default Parameter Values".…
cregox
  • 17,674
  • 15
  • 85
  • 116
27
votes
4 answers

Scala immutable map, when to go mutable?

My present use case is pretty trivial, either mutable or immutable Map will do the trick. Have a method that takes an immutable Map, which then calls a 3rd party API method that takes an immutable Map as well def doFoo(foo: String = "default",…
virtualeyes
  • 11,147
  • 6
  • 56
  • 91
25
votes
3 answers

Loading a resource to a mutable bitmap

I am loading a bitmap from a resource like so: Bitmap mBackground = BitmapFactory.decodeResource(res,R.drawable.image); What I want to do is make some changes to the bitmap before It gets drawn to the main canvas in my draw method (As it would…
stealthcopter
  • 13,964
  • 13
  • 65
  • 83
25
votes
1 answer

How do I pass a reference to mutable data in Rust?

I want to create a mutable struct on the stack and mutate it from helper functions. #[derive(Debug)] struct Game { score: u32, } fn addPoint(game: &mut Game) { game.score += 1; } fn main() { let mut game = Game { score: 0 }; …
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
25
votes
5 answers

Why is a string key for a hash frozen?

According to the specification, strings that are used as a key to a hash are duplicated and frozen. Other mutable objects do not seem to have such special consideration. For example, with an array key, the following is possible. a = [0] h = {a =>…
sawa
  • 165,429
  • 45
  • 277
  • 381
25
votes
9 answers

Why are Java wrapper classes immutable?

I know the usual reasons that apply to general immutable classes, viz can not change as a side effect easy to reason about their state inherently thread safe no need to provide clone/copy constructor/factory copy method instance caching no need…
shrini1000
  • 7,038
  • 12
  • 59
  • 99
24
votes
2 answers

Why do C# Arrays use a reference type for Enumeration, but List uses a mutable struct?

From what I've read, a design decision was made for certain Collections's Enumerator Types to be mutable structs instead of reference types for performance reasons. List.Enumerator is the most well known. I was investigating some old code that used…
Chuu
  • 4,301
  • 2
  • 28
  • 54
23
votes
4 answers

How to use mutable collections in Scala

I think I may be failing to understand how mutable collections work. I would expect mutable collections to be affected by applying map to them or adding new elements, however: scala> val s: collection.mutable.Seq[Int] = collection.mutable.Seq(1) s:…
Henry Henrinson
  • 5,203
  • 7
  • 44
  • 76
22
votes
2 answers

Converting immutable to mutable collections

What is the best way to convert collection.immutable.Set to collection.mutable.Set?
barroco
  • 3,018
  • 6
  • 28
  • 38
22
votes
2 answers

Convert immutable Bitmap file to mutable Bitmap

A: Bitmap immutableBmp= BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.sample); mutableBitmap=immutableBmp.copy(Bitmap.Config.ARGB_8888, true); B: Bitmap immutableBmp=…
Alex
  • 881
  • 2
  • 10
  • 24
21
votes
3 answers

mutable fields in Julia struct

I couldn't find an answer in both stackoverflow and the Julia docs to the following "design problem": Let's say I want to define the following object struct Person birthplace::String age::Int end Since Person is immutable, I'm happy that nobody can…
stefabat
  • 437
  • 3
  • 8
20
votes
2 answers

Why is MutableString deprecated in Python?

Why was the MutableString class deprecated in Python 2.6; and why was it removed in Python 3?
fjsj
  • 10,995
  • 11
  • 41
  • 57
20
votes
4 answers

Why does capturing a mutable struct variable inside a closure within a using statement change its local behavior?

Update: Well, now I've gone and done it: I filed a bug report with Microsoft about this, as I seriously doubt that it is correct behavior. That said, I'm still not 100% sure what to believe regarding this question; so I can see that what is…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
20
votes
5 answers

Passing parameters to lambda in C++

I seems to miss some point in lambda mechanism in C++. Here is the code: std::vector vec (5); int init = 0; std::generate(begin(vec), end(vec), [init]() mutable { return ++init; }); for (auto item : vec) { std::cout << item << "…
IgorStack
  • 799
  • 2
  • 6
  • 22