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

immer - Nested produce calls are not working as expected

const { produce } = require("immer"); const outer = produce((draft) => { return inner(draft); }, {}); const inner = produce((draft) => { draft.arr = [4, 5, 6]; }, {}); outer().arr.sort(); inner().arr.sort(); link:…
jaeyoi
  • 61
  • 2
2
votes
1 answer

How do immutable methods work in lodash fp?

I'd like to understand how the immutable methods of lodash/fp work. Do they deeply clone a collection before changing it or do they implement kind of a structural sharing between the two versions of the object? I have tried to understand it "from…
viebel
  • 19,372
  • 10
  • 49
  • 83
2
votes
1 answer

For Sets (immutable) and Strings (mutable), the behavior of "===" doesn't seem to comply to the documentation in Julia (v1.5 at least)

I think I am missing a point, as in my tests it seems the behavior of "===" doesn't comply to the documentation. The documentation states "First the types of x and y are compared. If those are identical, mutable objects are compared by address in…
GomsqB0T
  • 95
  • 9
2
votes
7 answers

How to pass references by value in C++?

I'm trying to create am immutable type (class) in C++, I made it so that all methods "aka member functions" don't modify the object and return a new instance instead. I'm running across a bunch of issues, but they all revolve around the reference…
hasen
  • 161,647
  • 65
  • 194
  • 231
2
votes
1 answer

Changing an immutable class in legacy code

I want to replace the (mutable) class class OldValue { private int value; public OldValue(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } with the…
MadScientist
  • 3,390
  • 15
  • 19
2
votes
1 answer

Can Such An Object Be Considered Immutable?

I have designed a class containing some information about a given object which will be registered in an SQL Server database. I would like to make this object (deeply) immutable, but I also must assure that it gets registered only once. If this…
User
  • 3,244
  • 8
  • 27
  • 48
2
votes
1 answer

Is a functional program already in SSA form?

Recently, I've been wanting to create a small (educational) functional, optimizing compiler. For the optimizing portion, I want to use SSA. The thing is that (most, as far as I know) functional programming languages have immutable variables (by…
2
votes
3 answers

C# - Are FieldInfo and PropertyInfo Immutable or Mutable?

Basically, I have the following: protected static readonly FieldInfo SpecialField = FindSpecialField(); FxCop is complaining to me though that I should not make a field readonly if it is mutable because the members can be changed. Are FieldInfo and…
michael
  • 14,844
  • 28
  • 89
  • 177
2
votes
2 answers

Returning pointer to intent(in) argument

I try to better understand the difference between const from C/C++ and intent(in) in Fortran in the context of classes. C++: If I const the implicit this argument to a member function and I return references to members of this, these references have…
mcocdawc
  • 1,748
  • 1
  • 12
  • 21
2
votes
2 answers

D language cast operator overload problem

I'm playing with D2 at the moment, I would like to write some simple program but i'm stuck with operator cast overload...I have a Vector class that can be cast to Normal : class Vector { public float x,y,z; this(in float x, in float y, in…
Andrea
  • 21
  • 1
2
votes
2 answers

How to check record in c# 9 in NET 5 is immutable at runtime

Record is a new feature in c#9, Net 5 It's said If you want the whole object to be immutable and behave like a value, then you should consider declaring it as a record Creating a record in c#9 , NET 5: public record Rectangle { public int…
M.Hassan
  • 10,282
  • 5
  • 65
  • 84
2
votes
2 answers

Is there a way to copy an arbitrary generator in Python?

Among the best-known features of functional programming are lazy evaluation and infinite lists. In Python, one generally implements these features with generators. But one of the precepts of functional programming is immutability, and generators are…
RussAbbott
  • 2,660
  • 4
  • 24
  • 37
2
votes
3 answers

In Java, is passing an object's non-primitive containing field to a method passed as an object handle and if so how does that affect its mutability?

If objects' non-primitive containing fields are passed as object handles that reference that field's object, is it susceptible to being changed after the fact if the originally passed field is updated/changed? public class MutableDog { public…
Snap
  • 492
  • 5
  • 14
2
votes
1 answer

Given a mutable class, how to make immutable a specific object of this class?

I got THIS class which is obviously mutable for every instance I create, but I want to know if there´s some kind of wrapper (or something) to make just ONE specific object of THIS class immutable. e.g Collections.unmodifiableList(beanList). class…
2
votes
0 answers

What is an accepted pattern for adding information to immutable types?

I'm writing some code which works on a collection and adds more information to it. It starts with A, and then adds some more information, say it calculates the "expiry" of A and returns that. Then I calculate the allowed permissions based on the…
Daniel James Bryars
  • 4,429
  • 3
  • 39
  • 57