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
13
votes
3 answers

How to convert a mutable HashMap into an immutable equivalent in Scala?

Inside a function of mine I construct a result set by filling a new mutable HashMap with data (if there is a better way - I'd appreciate comments). Then I'd like to return the result set as an immutable HashMap. How to derive an immutable from a…
Ivan
  • 63,011
  • 101
  • 250
  • 382
13
votes
1 answer

Difference between &mut and ref mut for trait objects

First of all, I'm not asking what's the difference between &mut and ref mut per se. I'm asking because I thought: let ref mut a = MyStruct is the same as let a = &mut MyStruct Consider returning a trait object from a function. You can return a…
Rodolfo
  • 335
  • 1
  • 4
  • 10
13
votes
1 answer

Changing class attributes by reference

I'm relatively new to Python and have problems with immutable variables. I'm trying to change the value of a class attribute (e.g. car.color). The difficulty is, that I can not use the namespace of car for doing this. Up to now I did not find a…
Jan
  • 171
  • 1
  • 1
  • 8
13
votes
5 answers

Logical const in D

D has two types of constness: immutable variables are ones that were declared immutable, and always will be immutable, while const variables are simply read only versions of an object. Logical const is when a function is marked as const, but allows…
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
13
votes
2 answers

static mutable member variables in C++?

why or for what reason is it not possible to declare a class member variable in C++ as static mutable? Something like static mutable int t; //This won't compile For me, there is no reason to ban such declarations. E.g. for reasons like maintaining…
shuhalo
  • 5,732
  • 12
  • 43
  • 60
13
votes
1 answer

What is difference between `mut a: &T` and `a: &mut T`?

Could someone explain what is the difference between these two and when is mut a: &T most often used?
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
13
votes
3 answers

Can I empty-base optimize mutable data?

I have a class template which looked like this: template class my_class { public: T f() const { resource_lock a_lock(some_mutex); return some_policy.some_operation(some_data); } private: …
sbi
  • 219,715
  • 46
  • 258
  • 445
13
votes
5 answers

Mutable class as a child of an immutable class

I want to have immutable Java objects like this (strongly simplified): class Immutable { protected String name; public Immutable(String name) { this.name = name; } public String getName() { return name; …
deamon
  • 89,107
  • 111
  • 320
  • 448
13
votes
5 answers

What to return when overriding Object.GetHashCode() in classes with no immutable fields?

Ok, before you get all mad because there are hundreds of similar sounding questions posted on the internet, I can assure you that I have just spent the last few hours reading all of them and have not found the answer to my…
Sheridan
  • 68,826
  • 24
  • 143
  • 183
12
votes
1 answer

Julia functions: making mutable types immutable

Coming from Wolfram Mathematica, I like the idea that whenever I pass a variable to a function I am effectively creating a copy of that variable. On the other hand, I am learning that in Julia there are the notions of mutable and immutable types,…
Dario Rosa
  • 251
  • 1
  • 6
12
votes
3 answers

Swift mutable set: Duplicate element found

My app uses mutable sets of custom elements. Once I had a crash with error „Duplicate element found in Set. Elements may have been mutated after insertion.“ Searching for an explanation, I found this post, which I don’t fully understand. My…
Reinhard Männer
  • 14,022
  • 5
  • 54
  • 116
12
votes
3 answers

Swift mutable structs in closure of class and struct behave differently

I have a class(A) that has a struct variable (S). In one function of this class I call a mutating function on struct variable,this function takes a closure. Body of this closure checks the struct variable's name property. Struct's mutating function…
tarun_sharma
  • 761
  • 5
  • 22
12
votes
4 answers

mutable fields for objects in a Java Set

Am I correct in assuming that if you have an object that is contained inside a Java Set<> (or as a key in a Map<> for that matter), any fields that are used to determine identity or relation (via hashCode(), equals(), compareTo() etc.) cannot be…
Jason S
  • 184,598
  • 164
  • 608
  • 970
11
votes
3 answers

How do you return multiple values and assign them to mutable variables?

This is what I have so far. let Swap (left : int , right : int ) = (right, left) let mutable x = 5 let mutable y = 10 let (newX, newY) = Swap(x, y) //<--this works //none of these seem to work //x, y <- Swap(x, y) //(x, y) <- Swap(x, y) //(x, y)…
Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
11
votes
4 answers

No Scala mutable list

Scala has both a mutable and an immutable Map , but it has only an immutable List. If you want a mutable List you need a ListBuffer. I don't understand why this is so. Any one knows?.
Sagar Varpe
  • 3,531
  • 4
  • 27
  • 43