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
16
votes
2 answers

How to check immutability

In python, is there any way to check if an object is immutable or mutable? like isimmutable(a) would return True, if a is immutable, else returns False.
rnbguy
  • 1,369
  • 1
  • 10
  • 28
15
votes
3 answers

a mutable type inside an immutable container

I'm a bit confused about modifying tuple members. The following doesn't work: >>> thing = (['a'],) >>> thing[0] = ['b'] TypeError: 'tuple' object does not support item assignment >>> thing (['a'],) But this does work: >>> thing[0][0] = 'b' >>>…
wim
  • 338,267
  • 99
  • 616
  • 750
15
votes
1 answer

How to get a List of (immutable and mutable) Sets in scala?

I try to build a list of (mutable and immutable) Sets. The compiler gets into trouble as it cannot figure out the type of that list. I always thought that I can connect Lists of any types and that the type of the new List is a kind of supertype of…
Jan
  • 153
  • 3
15
votes
5 answers

Sort data from a MutableList in Kotlin

I'm new to Kotlin and need to ask some questions about ordering a MutableList. As I understand it, I can do a myMutableList.sortBy {it.int} and a myMutableList.sortByDescending {it.int} for both Int and String. But return is always a Unit…
VTR2015
  • 429
  • 2
  • 6
  • 15
15
votes
1 answer

Why is mutable specifier classified to be storage class specifier, but not a qualifier?

When the mutable specifier is used in the declaration of a non-static data member, the data is mutable no matter whether the rest of the object is treated as const. With this fact, we may easily have the impression that mutable specifier is the…
user2486888
15
votes
4 answers

What is the proper way to remove elements from a scala mutable map using a predicate

How to do that without creating any new collections? Is there something better than this? val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4) m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1)) println(m) P.S.…
Oleg Galako
  • 1,036
  • 1
  • 10
  • 16
14
votes
4 answers

Can't append to scala's mutable LinkedList?

I'm looking at the API and the :+ method returns a new LinkedList. The append method will only allow the appending of another linked list. The += method needs a var to work. Why would anyone ever need these if the LinkedList is mutable? What…
dpington
  • 1,844
  • 3
  • 17
  • 29
14
votes
1 answer

C++ How affects mutable keyword to the performance of container?

I want to know how mutable affects a container (map, vector, list, ...). In addition, what do I have to bear in mind?
tonnot
  • 435
  • 1
  • 5
  • 17
14
votes
5 answers

How to type mutable default arguments

The way to deal with mutable default arguments in Python is to set them to None. For example: def foo(bar=None): bar = [] if bar is None else bar return sorted(bar) If I type in the function definition, then the only type for bar says that…
Pro Q
  • 4,391
  • 4
  • 43
  • 92
14
votes
3 answers

Why can I not return a mutable reference to an outer variable from a closure?

I was playing around with Rust closures when I hit this interesting scenario: fn main() { let mut y = 10; let f = || &mut y; f(); } This gives an error: error[E0495]: cannot infer an appropriate lifetime for borrow expression due to…
soupybionics
  • 4,200
  • 6
  • 31
  • 43
14
votes
1 answer

MobX Mutability vs Immutability

Why MobX encourage mutable objects in their docs?? But, I see a tutorial about MobX: http://orlandohamsho.com/javascript/mobx-react-tutorial-building-first-application/ And, he used immutable approach in his tutorial instead of mutable one (see…
Terry Djony
  • 1,975
  • 4
  • 23
  • 41
14
votes
1 answer

Mutable borrow in loop

I am trying to get a mutable borrow inside a loop, and I cannot get it to work. I've tried all the possible guards, raw pointers, everything. struct Test<'a> { a: &'a str, } impl<'a> Test<'a> { pub fn new() -> Self { Test { a: &mut…
Chronium
  • 906
  • 9
  • 12
14
votes
6 answers

NSMutableDictionary: mutating method sent to immutable object

The following code is returning an exception with the following error message "mutating method sent to immutable object" when attempting to removeObjectForKey NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults…
Remixed123
  • 1,575
  • 4
  • 21
  • 35
14
votes
2 answers

Does it make sense to modify in-place AND return a copy?

Note: I'm tagging this Python and C++ because I've seen examples in both, but the question is language-agnostic. A function or class method that modifies an object has two choices: modify the data directly in the object in question, or create a new…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
14
votes
2 answers

How do you declare the values of a dictionary entry as mutable?

The Google yields plenty of example of adding and deleting entries in an F# dictionary (or other collection). But I don't see examples to the equivalent of myDict["Key"] = MyValue; I've tried myDict.["Key"] <- MyValue I have also attempted to…
telesphore4
  • 877
  • 1
  • 7
  • 19