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
9
votes
1 answer

How do I handle/circumvent "Cannot assign to ... which is behind a & reference" in Rust?

I'd implementing a simple linked list. This is the (working) code I had so far: pub struct LinkedList { start: Option>>, } impl LinkedList { pub fn new() -> LinkedList { return LinkedList { start: None }; …
Islion
  • 111
  • 1
  • 6
9
votes
3 answers

Sonar - Store a copy - Mutable members should not be stored or returned directly

I have a list which is a private member in my class. I have used getter and setter to get and set the values. SOnar throws an error - Mutable members should not be stored or returned directly. For example: ABC and DEF are two classes. class…
pgman
  • 493
  • 3
  • 12
  • 21
9
votes
2 answers

Why doesn't the compiler report an error when a variable not declared as mutable is modified?

I installed Rust 1.13 and tried: fn main() { let x: u32; x = 10; // no error? } When I compiled this file there's some warnings, but there's no error. As I'm not declaring x as mut, shouldn't x = 10; cause an error?
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
9
votes
2 answers

How to check a type for mutability

Is there a way to check if a type is mutable or immutable? Can this check be done at compile-time (i.e. have branches if ismutable(T) compile away to just use the codepath for either mutability or immutability)?
Chris Rackauckas
  • 18,645
  • 3
  • 50
  • 81
9
votes
3 answers

Java Collections with Mutable Objects

How does a TreeSet, HashSet or LinkedHashSet behave when the objects are mutable? I cannot imagine that they would work in any sense? If I modify an object after I have added it; what is the behaviour of the list? Is there a better option for…
laurencer
  • 1,680
  • 1
  • 19
  • 29
9
votes
5 answers

Const method that modifies *this without const_cast

The following pattern has arisen in a program I'm writing. I hope it's not too contrived, but it manages to mutate a Foo object in the const method Foo::Questionable() const, without use of any const_cast or similar. Basically, Foo stores a…
AshleysBrain
  • 22,335
  • 15
  • 88
  • 124
9
votes
2 answers

C# How can I tell if an IEnumerable is Mutable?

I want a method to update certain entries of an IEnumerable. I found that doing a foreach over the entries and updating the values failed as in the background I was cloning the collection. This was because my IEnumerable was backed by some LINQ->SQL…
Logan
  • 291
  • 1
  • 5
  • 10
9
votes
7 answers

Should this immutable struct be a mutable class?

I showed this struct to a fellow programmer and they felt that it should be a mutable class. They felt it is inconvenient not to have null references and the ability to alter the object as required. I would really like to know if there are any other…
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
9
votes
6 answers

Is this object mutable?

If I have a class like that: public class MyObject { private int myField = 2; public void setMyField(int f) { this.myField = f; } } Will objects of this class be mutable? Thanks!
Roman
  • 199
  • 1
  • 5
9
votes
2 answers

How to write this C# code in F#

I'm used to write code like this in C#: SomeObj obj; try{ // this may throw SomeException obj = GetSomeObj(); }catch(SomeException){ // Log error... obj = GetSomeDefaultValue(); } obj.DoSomething(); This is the way I translated it…
Gerardo Contijoch
  • 2,421
  • 5
  • 20
  • 29
9
votes
1 answer

A bug of mutable.Set.foreach in scala?

I'm using scala 2.9.1, when I try this code: import scala.collection.mutable val a = mutable.Set(1,2,3,4,7,0,98,9,8) a.foreach(x => { println(x); a.remove(x) }) the result was something like 0 98 2 1 4 3 8 which did not list all the elements of a.…
user1923692
  • 111
  • 3
9
votes
2 answers

What's the most efficient way to make immutable and mutable versions of an objective-c class?

Suppose I’m making an Objective-C class that represents a fraction, and want to create immutable and mutable versions. Following the patterns in the Foundation framework, you might expect to see the method fractionByAddingFraction: in the immutable…
8
votes
6 answers

The final word on NSStrings: Mutable and Immutable

I've read in several books... and online... about immutable and mutable strings. They claim "immutable strings" can't be changed. (But they never define "change".) Which of these NSStrings could be changed without using NSMutableString? The string…
Donna
  • 237
  • 1
  • 4
  • 7
8
votes
3 answers

advantages of stateful programming?

i was wondering about the benefits of stateless programming, and found someone who shared my question: Advantages of stateless programming? as i read through the answers though, it made me curious about the converse question. what are the…
8
votes
4 answers

Can I always convert mutable-only algorithms to single-assignment and still be efficient?

The Context The context of this question is that I want to play around with Gene Expression Programming (GEP), a form of evolutionary algorithm, using Erlang. GEP makes use of a string based DSL called 'Karva notation'. Karva notation is easily…