Questions tagged [immutability]

Immutability is the inability to modify data after it has been created. Modifications are instead made by copying the data. A property of immutable data is that it is *referentially transparent*.

Overview

Immutability is the inability to modify a variable after it is has been created.

It is a pattern found in many branches of programming; immutable objects are used widely within object oriented languages (such as Python's str type, Java's String and Integer type, .NET's System.String, etc.), functional programming (esp. Haskell and other pure languages), and other paradigms.

See also

3670 questions
2
votes
1 answer

How to refer to the current state in FP?

I've already asked that question but my explanation was pretty bad, so I decided to ask again with a better explanation and with actual code (I'll ask moderators to delete one of the posts). So let's consider the problem. Following snippet…
2
votes
1 answer

React inmutability challenge: Immutable array with concatenates

I tried everything to make this inmutable without success: The array that will change: const [answers, setAnswers] = useState([]); A button that runs my loop onClick={ async() => my function} A function that runs N requests, one by one and updates…
Loïc V
  • 517
  • 7
  • 9
2
votes
1 answer

Why are some Kubernetes' resources immutable after creation?

From the Kubernetes' validation source code, at least those resources are immutable after creation: Persistent Volumes Storage Classes Why is that ?
Amine Zaine
  • 165
  • 1
  • 21
2
votes
2 answers

Naming convention for nonmutating 'add' to a collection

I have a class called Grammar, which holds a collection of production rules. Before doing things with a Grammar object, I usually need to add a few extra rules to it, which should be forgotten after the current task is done. So, I'd like to add a…
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
2
votes
1 answer

In Haskell, how to distinguish mutable references vs regular variables in monadic functions definitions

Let's say you want to write some stateful function in Haskell. You have to use a monadic style like this: (using whatever state monad) f :: x -> k -> (ST s) r So this means that essentially the function takes some input x and k, might use and/or…
jam
  • 803
  • 5
  • 14
2
votes
2 answers

copy() method in Java - any such thing?

I am following the well famous IBM tutorial on parsing an RSS feed. I tested it and I am getting a list of the first item only. In the AndroidSaxFeedParser, we can see that currentMessage is final which means that it can't be changed, and when I…
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158
2
votes
1 answer

Create immutable HashMap from Cargo build script

I'm using something similar to this answer to load some file data into a Rust binary. I'd like this data to be stored in a HashMap, so I'm able to search by key in the main program. However, I'm not sure how to do it in a way that ensures this data…
DavSanchez
  • 831
  • 8
  • 13
2
votes
0 answers

Which sorts of side effects are caused by mutations?

Mutable data types cause side effects, but what side effects specifically and how can they be grouped? So far I've found two sorts of effects: race conditions (due to single threaded JS in the context of asynchronous computations/event…
user5536315
2
votes
1 answer

When operating on immutable objects in Python, how is the new object created?

I understand that immutable objects cannot be changed in place. A new object is created and reassigned to the same variable name. In this way, we can maintain the association to the variable. Internally, the variable points to a different object.…
bhp
  • 101
  • 1
  • 7
2
votes
1 answer

How to implement immutable and read-only versions of mutable objects effectively?

Context : package data, public : public interface _Data { public String getData(); } public class _PackageAPI { DataHolder holder; public void createHolder(String data) { holder = new DataHolder(); …
user13190396
2
votes
3 answers

Set multiple const values based on conditional

I want to set multiple values based on a conditional. This code would work: let a; let b; if (fooBar) { a = "foo"; b = "bar"; } else { a = "baz"; b = "Hello world!"; } But I am trying to adhere to FP (immutable variables) and DRY…
2
votes
1 answer

In the absence of mutable types, is there a case for invariant type parameters?

Java Arrays are not fully type-safe because they are covariant: ArrayStoreException can occur on an aliased array. Java Collections, on the other hand, are invariant in their type parameter: e.g., List is not a subtype of List
raner
  • 1,175
  • 1
  • 11
  • 21
2
votes
2 answers

Immutable classes and memory in C++

So, I'm getting deeper into C++ than I did in school as part of a personal project I'm working on. I'm a Java developer, so memory management is a little hard to get used to again, and now that I'm going out of my way to code a certain way, I have a…
monitorjbl
  • 4,280
  • 3
  • 36
  • 45
2
votes
1 answer

Create Immutable Object in Python

I want to create an immutable class like dictionary in python. I have found the following solution on stackoverflow, but this object is value can be updated by using __dict__.update function. Is there way to block this action. class…
user5828964
  • 302
  • 2
  • 10
2
votes
3 answers

Python static immutable properties

What's the correct way to implement static immutable properties in Python? Minimal example: A program module 'Family' has a class Parent, defined below: class Parent(): def __init__(self, type): self.type = type The parent can be of…
Voy
  • 5,286
  • 1
  • 49
  • 59