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*.
immutability
3,712
questions
1
vote
1
answer
32
views
What's the workaround for Django/PostgreSQL GeneratedField error
When defining a GeneratedField with PostgreSQL, it is not uncommon to encounter the following error: django.db.utils.ProgrammingError: generation expression is not immutable.
This error is mentioned ...
0
votes
0
answers
46
views
Function adapting to mutable or non-mutable input
I wanted to build a function/method that could adapt if a non-mutable or mutable reference was provided.
I tried something like this (playground):
trait TryAsMut<'a, T> where T: 'a {
fn ...
0
votes
1
answer
48
views
What prevents JavaScript primitives from being mutated?
In C, after declaring and initializing a primitive, we're able to access the memory address where the primitive is stored and directly modify its value. However in JavaScript it is said that primitive ...
1
vote
1
answer
98
views
Java List.of() as key in HashMap?
I know that in general, you should only use immutable objects as a key to a hashmap since if their address or their contents change, then the hash breaks. This is one of the reasons why a normal List ...
0
votes
1
answer
74
views
Kotlin backing field wrong type
ViewModel
private val _wordPressPostsState = Channel<WordPressPostsState>()
val wordPressPostList: List<WordPressPostDataDomain>
field = mutableListOf<WordPressPostDataDomain>()
...
0
votes
1
answer
38
views
How to use Kotlin backing field with Channel and Flow?
I am playing with the new backing field feature introduced in Kotlin 2.0, however I am encountering some problem when using it on data type that is not a superclass/subclass of one and another.
...
3
votes
1
answer
101
views
Why does `Option::map` work with immutable data, but not with mutable data
Why does the immutable version in the following example work, but the mutable version does not compile with error error: lifetime may not live long enough?
trait Car {
fn honk(&self);
}
...
1
vote
2
answers
96
views
Why thread safety is listed as a benefit of immutability?
I was reading about Records in C# and read that they are immutable. And thread safety is written as a benefit of immutability. How immutability provides thread safety? I tried a code sample claiming ...
1
vote
1
answer
158
views
Angular signals and immutable update of nested objects
I have 2 classes:
export class Enrollment {
private _productId = signal(0);
private _price = signal(0);
public productId = this._productId.asReadonly();
public price = this._price....
0
votes
0
answers
22
views
ReactJS: updating the state of arrays in ReactJS, the question has nothing to do with objects [duplicate]
I am trying to add an item to an array in ReactJS code, but the new item (greetings) does not add to the array, what would be the potential error in the blow code?
import { useState } from "react&...
0
votes
1
answer
45
views
Update references to immutable object
Please, imagine a very simple (and awkward) concept. I have created it to demonstrate a much complex problem.
Lets have immutable class Customer. Then we have a object order of class Order that ...
0
votes
0
answers
22
views
Issues with nested state updates on mutually related objects using Immer.js: changes not reflecting properly
A partner and I are currently working on a side project that aims at creating a React state management library that uses Immer.js to ensure immutability. The end goal is to be able to handle complex ...
0
votes
1
answer
138
views
Cast non-primitive type in Rust
I am very new to Rust, so apologies if this is a simple question!
I have the following simplified scenario, where I have a trait and two struct types (TypeA and TypeB) that implement the trait. I then ...
0
votes
3
answers
91
views
Why have I mark a string field as readonly in a immutable class if strings are immutable [duplicate]
I have an immutable class with only immutable properties. Strings are immutable so why I have to mark string field as readonly or use "init" in that class?
public class ImmutableType
{
...
0
votes
1
answer
278
views
kotlinx.collections.immutable Persistent vs Immutable collection
Can someone give an example showing how a persistent collection is different from an immutable collection in kotlinx.collections.immutable?
I've found this explanation, however I don't fully ...