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 use nested JsonConverters?

I´m using the new System.Text.Json library trying to deserialize some json directly into immutable classes. using System.Collections.Immutable; public sealed class Book { public int Id { get; } public string Name { get; } public…
JakobFerdinand
  • 1,087
  • 7
  • 20
2
votes
2 answers

After assigning a Map to ImmutableMap, how to stop or restrict reassign another map

I declared a ImmutableMap like public static ImmutableMap mapImmutable; Assigned a map to this variable mapImmutable= ImmutableMap.copyOf(map2); Now if I assign other map to this 'mapImmutable' variable. It will not throw any…
2
votes
1 answer

Immutability should throw error but it does not in string

I am testing immutability of a string and wrote this function that takes in a String. Since a string is immutable, str[i] below, cannot be assigned to any value. However, when I run this function, it does not give me an error. Why is that? (as per…
Snowcat
  • 470
  • 8
  • 16
2
votes
2 answers

Redux updating nested data [Immutable Update Patterns]

Can anyone help with this update pattern. I am not using any libraries like immer. I have to update a nested object and the data looks like dis Sample data { isFetching: false data:{ nba : { stack :{ …
Hemanthvrm
  • 2,319
  • 1
  • 17
  • 26
2
votes
2 answers

State with IO in Haskell for CLI application

I am trying to write a simple implementation of the game Hangman in Haskell and one problem I have is keeping the state of the game. A simplified / abstract version of my problem looks like this: advanceState :: Char -> [Char] -> [Char] advanceState…
Niklas Vest
  • 872
  • 6
  • 20
2
votes
4 answers

Immutability and updating nested key of local state

I have the following state. state = { friends: { nickNames: ['Polly', 'P', 'Pau'], ... here more k:v }, } } And I want to update nickNames array with a value coming from an uncontrolled form through a method in my class. However,…
Peter
  • 2,004
  • 2
  • 24
  • 57
2
votes
1 answer

Can I have StringBuilder in an immutable class

If I create an immutable class. All the fields have to be final. If I use stringbuilder in that like this final StringBuilder s = new StringBuilder("Hello "); , then the append function can append the value of the s and the class wont be…
2
votes
1 answer

How do i implement lazy initialization and caching in an immutable c++ object?

I've learned about the 'Elegant Objects' principles (see elegantobjects.org) a while back and they are easy enough to follow in C#, but now that i'm doing some c++ the immutability is causing me some trouble. I'm looking for best practices on how to…
2
votes
1 answer

"We're immutable, so use __new__ not __init__"

I found this phrase in module fraction.py of standard library: class Fraction(numbers.Rational): ... # We're immutable, so use __new__ not __init__ def __new__(cls, numerator=0, denominator=None, *, _normalize=True): …
sanyassh
  • 8,100
  • 13
  • 36
  • 70
2
votes
2 answers

How does readIORef work: creates copy or it does not?

What exactly does this code do? Is someMap a copy of the object (of ::Data.Map.Strict.Map) referred to by myMap or it's a reference only? I mean can someMap change (by another thread) after I read it with readIORef? Something like C's volatile... Is…
RandomB
  • 3,367
  • 19
  • 30
2
votes
0 answers

"real world" JSON transformation using immutable javascript/node

Background I transformed a non-trivial JSON response that uses arrays into a nested object eliminating some of the original properties inserting a new summary property that is a unique list of values from the original object I did this with…
Timothy Vogel
  • 1,363
  • 2
  • 21
  • 39
2
votes
3 answers

Any nice way to make a chain of immutable objects loop around?

This question is an extension of this question. I have a class similar to the following. class HighlightableStructure { private final HighlightableStructure NEXT; HighlightableStructure(HighlightableStructure next) { NEXT…
Lindstorm
  • 890
  • 2
  • 7
  • 22
2
votes
2 answers

Primary constructor parameter declared using val allows to change the value

As shown in below code parameters in primary constructor are defined with default values and val it means values of those parameters should not change. But still why the values changing while initializing the constructor //Why values of Aname and…
Rahul Wagh
  • 281
  • 6
  • 20
2
votes
3 answers

How do you avoid code duplication in an immutable data model with inheritance?

Given a class hierarchy such as: Entity { id, name, position } Combatant : Entity { health, strength } Avatar : Combatant { connection } Which are all immutable. To implement 'move' on an entity I can return a new entity with a different…
Timothy Pratley
  • 10,586
  • 3
  • 34
  • 63
2
votes
4 answers

Design of immutable and mutable objects in Java

My problem concerns an API design. Let's say I'm designing a vector (math/physics meaning). I would like to have both an immutable implemenation and a mutable one. I have then my vector that looks like this: public interface Vector { public float…
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
1 2 3
99
100