Questions tagged [mutability]

Mutability is property of any function, variable or expression whose values are subject to changes with "side-effects". In other words, the value does not have referential transparency.

Overview

Mutability is property of any function, variable or expression where values are subject to changes with "side-effects". In other words, the value does not have referential transparency.

When the property is present on any component of a program, that program component is said to be "mutable".

Mutability may also generally refer to the question of whether or not a program component is mutable. When a program component is not "mutable" it is said to be "immutable".

See also

304 questions
9
votes
4 answers

Command Pattern vs. Visitor Pattern

Is it generally acceptable to allow a Visitor to modify state of the Receiver, or should that be a Command pattern instead?
grefly
  • 1,181
  • 2
  • 12
  • 28
8
votes
1 answer

Publishing Non-Thread Safe Object Fields in a Thread-Safe Manner

I've got a problem with Java concurrency. Yes, I looked at questions with almost the exact same title, but they all seemed to be asking subtly different things. Yes, I've read Java Concurrency in Practice. Yes, I can see why it's the defacto…
Louis
  • 2,442
  • 1
  • 18
  • 15
8
votes
6 answers

Mutable and Immutable Strings in python

I was just going through mutable and immutable structures in python. It was written that "Strings are immutable" in Python i.e We cannot alter them Consider the code: str1='Rohit' str1.replace('R','M') This gives the output: 'Mohit' Now, somebody…
user8914517
8
votes
3 answers

What is the difference between returning an Array and an IList<>? (RE: Eric Lippert's harmful arrays)

I just read Eric Lippert's "Arrays considered somewhat harmful" article. He tells his readers that they "probably should not return an array as the value of a public method or property," with the following reasoning (slightly paraphrased): Now the…
kdbanman
  • 10,161
  • 10
  • 46
  • 78
8
votes
2 answers

Mutability design patterns in Objective C and C++

Having recently done some development for iPhone, I've come to notice an interesting design pattern used a lot in the iPhone SDK, regarding object mutability. It seems the typical approach there is to define an immutable class NSFoo, and then derive…
Mac
  • 14,615
  • 9
  • 62
  • 80
8
votes
3 answers

What are the semantic implications of :volatile-mutable versus :unsynchronized-mutable?

I was studying a clojure lib when I noticed that a mutable field was annotated with ^:unsynchronized-mutable. Mutable is mutable, but I had no idea what the unsynchronized part meant, so I read the docs, which contain: Note well that mutable fields…
konr
  • 2,545
  • 2
  • 20
  • 38
7
votes
3 answers

2 questions at the end of a functional programming course

Here seems to be the two biggest things I can take from the How to Design Programs (simplified Racket) course I just finished, straight from the lecture notes of the course: 1) Tail call optimization, and the lack thereof in non-functional…
7
votes
3 answers

Why do changes to a nested dict inside dict2 affect dict1?

I don't understand these cases: content = {'a': {'v': 1}, 'b': {'v': 2}} d1 = {'k1': {}} d2 = {'k2': {}} d1['k1'].update(content) print(d1) content['a']['v'] = 3 content['b']['v'] = 4 d2['k2'].update(content) print(d2) print(d1) >>> {'k1': {'a':…
Marc
  • 1,340
  • 2
  • 14
  • 23
7
votes
5 answers

Immutable views of mutable types

I have a project where I need to construct a fair amount of configuration data before I can execute a process. During the configuration stage, it's very convenient to have the data as mutable. However, once configuration has been completed, I'd…
Dan Bryant
  • 27,329
  • 4
  • 56
  • 102
7
votes
1 answer

Mutable vs Ref variables in terms of capture

My superficial understanding of variables in f# suggests that declaring a variable to be 'mutable' and using a 'ref' variable essentially both do the same thing. They are both different ways to address the same underlying issue - a limited and…
Richard Warburton
  • 1,462
  • 1
  • 9
  • 13
7
votes
1 answer

scala mutable val List

Few days ago I found Paul Philip’s gist https://gist.github.com/paulp/9085746 which shows quite strange behavior. I did not find any explanation how is that possible simplified code snippet: val buf = new ListBuffer[Int]() buf ++= Seq(1,2,3) val…
Sergey Nesteruk
  • 257
  • 1
  • 2
  • 8
7
votes
2 answers

Local reference to inner array in swift

I came from C# world, and used to arrays being reference types. As I understand, in swift arrays are value types, but they try to play as reference ones. I don't actually know how to ask what I need (I think this is the case when I need to know…
Lanorkin
  • 7,310
  • 2
  • 42
  • 60
7
votes
1 answer

Parsing data into a module-level mutable static variable

I have a set of functions within a module that need access to some shared initialization-time state. Effectively I'd like to model this with a static mutable vector like: static mut defs: Vec = vec![]; fn initialize() { …
Bosh
  • 8,138
  • 11
  • 51
  • 77
7
votes
1 answer

Passing an array as a function argument from within a function which takes it as an argument in C

G'day! If I have a function which takes an array of ints as an argument, and then from within that function, send off that same array to another function, will it still be able to edit the array values and have them be committed at a main level…
James Adams
  • 678
  • 4
  • 11
  • 21
7
votes
1 answer

C# mutability - VS Code Analysis giving me CA2104? Seems... poor. Am I misunderstanding?

In C#, I want to make "smart" enums, kind of like is possible in Java, where there's more information attached to an enum value than just the underlying int. I happened upon a scheme of making a class (instead of an enum), as in the following…
1 2
3
20 21