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*.
3,686
questions
0
votes
1
answer
62
views
Using the parser combinator crate "nom" to partially read and parse a file
I have a usecase where I only want to parse the beginning of a file to to the point where I decide to stop parsing. This can be 4kB or more. The exact amount is not known. So there is no need to read ...
0
votes
2
answers
64
views
Convert dataclass containing set of another dataclass to nested dictionary
I have the following dataclasses:
# model.py
from dataclasses import dataclass
from typing import Optional
@dataclass(frozen=True)
class Location:
x: int
y: int
def __lt__(self, other):
...
0
votes
1
answer
25
views
Cannot extend nor lock Azure immutability policy from command line: Operation not allowed on immutability policy with incorrect etag
I am using Azure immutable storage, aka WORM (Write Once, Read Many).
I am trying to set the immutability policies using the az command line, as documented here. I can create policies from the CLI, ...
0
votes
1
answer
70
views
How to create an efficient immutable tree, with parent pointers
I'm constructing a "scenegraph", which is a hierarchical data structure of Shape nodes (e.g. sphere, cube, mesh, etc. not shown in example code). A Shape can own zero or more child Shape ...
0
votes
0
answers
47
views
Record types, memorry usage and copy operations
If I define a record in C# like
public record Pair(int First, int Second);
and I have a loop where I do
Pair x = new(1, 2);
while (...)
{
x = new(x.First + 1, x.Second + 1);
}
my IDE highlights ...
0
votes
2
answers
63
views
How immutable objects' memory cleared?
I have some questions-
As string constant pool is present in the heap memory and GC collects garbage from heap memory.
Does it cleans the string constant pool as well, according to my info it does ...
0
votes
2
answers
88
views
Making a counter with a closure in Elixir
I'm learning Elixir and I just got to the part about closures. When a language has closures, the first thing I usually do is try to make the closure algorithm. In JavaScript it looks something like ...
-3
votes
0
answers
46
views
what is mean by immutable in stringbuffer of java? [duplicate]
as we know string in java is immutable than why printing (s = dzzzzz )(https://i.stack.imgur.com/B2Cp0.png)
thinking result should be an error as string is immutable and string buffer is mutable.
...
-2
votes
1
answer
46
views
Leetcode, works in test case but not when submitted
Hi this is my first question in StackOverflow, so it may not be perfect.
I'm trying to solve this question from leetcode using python3.10
https://leetcode.com/problems/encode-and-decode-tinyurl/
My ...
1
vote
1
answer
76
views
What does target and object mean in assignment statements?
https://docs.python.org/3/library/copy.html#module-copy
Assignment statements in Python do not copy objects, they create
bindings between a target and an object. For collections that are
mutable or ...
0
votes
1
answer
37
views
As tuples are immutable, does it mean that in a database when a value in a row is modified the tuple containing this value is entirely reassigned?
A record in a database is a tuple. A tuple is immutable. When a value of a record is changed , is the tuple entirely reaffected ? If so, is it still efficient (I guess yes) ? Why this data structure ...
0
votes
0
answers
52
views
How to return an unmodifiable Map in Java? [duplicate]
java.util.Collections has the unmodifiableMap method, which creates a view of the map, making it impossible to alter the map.
But why isn't there an ImmutableMap type? As the unmodifiableMap return is ...
2
votes
0
answers
184
views
Android 14 flag mutable
I'm working to support android 14 in a certain app, i'm having one issue with android native share sheet, since we have a pending intent with FLAG_MUTABLE, i'm getting this error
2023-10-16 14:23:20....
1
vote
3
answers
59
views
Having a mix of mutable/immutable "data container" python
I was wondering if there's a way to define within the same "data container" values of mutable/immutable/default values.
by "data container" I mean any things like tuple, dict, ...
0
votes
0
answers
23
views
Unable to modify String variable through method in Java [duplicate]
I am currently learning Java, but am having issues understanding how to use a method to modify the object a string variable points towards.
I am working on making a code to return the bigger and ...
0
votes
2
answers
117
views
How to protect class instance against alteration
How can I modify the body of the code (this can include the body of the constructor) to protect Person instances against alteration: Once a Person is created, it should not be possible to modify it.
...
1
vote
1
answer
84
views
Protect individual values in std::array while allowing complete overwrite
I have an array which is global state. This is running in an embedded/microcontroller environment, not a large application where I might be more concerned about global state.
How can I declare the ...
-1
votes
1
answer
64
views
C# object's returning internal components
Goal
A very recurrent design I would like to implement in C# is following: A class, that owns multiple instances of another class.
For clarity let's pick an example, let's say a "car" that ...
0
votes
0
answers
37
views
Why does rust allow us to store mutablre referneces to a immutable variable? [duplicate]
In rust, if we can store mutable reference to a mutable variable, like so:
fn main() {
let mut str: String = String::new(); // a mutable string
let ref1 = &mut str; // storing a mutable ...
0
votes
0
answers
85
views
Return an immutable vector of unique_ptr
I have the following object:
class Container {
public:
std::vector<std::unique_ptr<Item>>& items() { return m_items; }
private:
std::vector<std::unique_ptr<Item>> ...
0
votes
0
answers
20
views
React - Child components not updating when setting the parent's state too quickly
I have a big form with around 1000 dynamically rendered inputs (based on a database table) and each have their own reference in the parent's state array inputs. Here is the structure of said array :
[
...
0
votes
1
answer
29
views
In React functional components, if I just make a new copy of array but keep the objects, does it break the pattern?
Let's say if my app or component has an array of personInfo.
const [persons, setPersons] = useState([{ name: "", phone: ""}]);
so that I can add a person, remove a person, edit ...
1
vote
3
answers
69
views
Make polyfills immutable
For example:
Object.defineProperty(Promise.prototype, 'onFinally', {
get: () => {},
writable: false,
});
or
Object.freeze(Promise.prototype);
These examples aren't work, is there a working ...
1
vote
1
answer
49
views
Deep Copy of Cons instance of Java List interface
I am implementing a immutable List interface in java, a Cons class, essentially a list of lists ie; Cons(1,2,3)= Cons(1,Cons(2,Cons(3,null)))
I am trying to create a deepcopy of a cons but have had no ...
4
votes
1
answer
40
views
How can non-built-in type instances be immutable in python?
If I try to run
import numpy as np
type(np.dtype).moo = 7
then I get
TypeError: cannot set 'moo' attribute of immutable type 'numpy._DTypeMeta'
which I can't find an explanation for.
numpy....
-3
votes
1
answer
63
views
Naming convention for a method that create a new object from the current instance but modifies some properties [closed]
I was wondering if there are any common conventions for a method that:
returns a new instance of the same type of object;
using the current instance's property values as the defaults;
while changing ...
0
votes
0
answers
19
views
What is an immutable string? [duplicate]
I’m very new to coding and want to know what an immutable string is. I’ve read that this means that a string object can’t be changed once it’s created.
String = ‘hello’
String = ‘bye’
Would the second ...
0
votes
1
answer
53
views
Usage of Equatable in Flutter Dart
Im currently working on a Flutter school project.
Im totaly new to Flutter and Dart and have troubles to understand the usage of Equatable package.
I build my Flutter Application using the Bloc-...
0
votes
0
answers
28
views
Handling frequent updates in data layer, isn't immutabability a performance problem?
Lets say I have a list of many items, displaying on the screen, e.g.
list with three sliders, showing the value
In my understanding, a typical architectural pattern with Jetpack Compose contains ...
1
vote
2
answers
74
views
Lombok Value annotation not working properly for Immutable java class
I was looking to see if it's possible to create an immutable Java class using Lombok. I found this page on the Lombok website "Immutable classes made very easy", but I'm not convinced by it: ...
1
vote
2
answers
46
views
Why after calling "defineProperties", my object became empty
I have this object
const product = {
name: "Laptop",
price: 1000,
quantity: 5
};
console.log(product) //{ name: 'Laptop', price: 1000, quantity: 5 }
but then I should to ...
0
votes
3
answers
68
views
Python mutable/immutable and local variables
I am quite confused about a difference in behavior of the two following code snippets, and in particular about expectations of their behavior in the mutability/immutability in Python concept:
Snippet ...
2
votes
1
answer
69
views
Shallow Copying Class Objects vs. String Variables
string a = "John";
string b = "Doe";
a = b; // Shallow copying strings
b = "Elon Musk";
Console.WriteLine(a); // Output: Doe
This prints "Doe", meaning the ...
1
vote
0
answers
40
views
Unable to render images to a PPTX slideshow using Apache POI
I have this code that reads a .pptx template file to fetch placeholder mappings and then subsequently replace these placeholders with actual data. It uses Apache POI in Scala.
Here is my code:
def ...
-1
votes
2
answers
58
views
Can I annotate a member field as read only or immutable in Python? [duplicate]
I am not looking for a "physically" immutable member field in Python as I know it is
impossible, but just a type annotation to tell type checker that this field should not be re-assign a new ...
0
votes
0
answers
16
views
Why a String which by definition is immutable can change its size?
I know that String is immutable which means once created it cannot change its size (can't become larger or smaller) or it cant be changed any of its characters. Now, I found 2 examples and it is a bit ...
0
votes
0
answers
42
views
Why is my array only storing one item and replacing the old item with the new one?
I have a checklist component on a React form. When an item is checked, I want to immutably add it to checkboxVal state array, when unchecked, I want to remove it. Currently, when I check a new value, ...
0
votes
2
answers
77
views
Scala val and var with collections
I want to understand better the following issue.
When should i use var and when val?
I know that there is a rule of thumb in Scala that we should use val.
For primitive types it's easy - we should use ...
1
vote
1
answer
60
views
I can write an immutable variable in rust
let mut a = Box::new("123".to_string());
let b = Box::new( &mut a);
b.push('4');
assert_eq!( "1234", b.as_str());
// lets see the types:
// let x001: Box<&...
0
votes
0
answers
127
views
How are python lists and tuples stored in memory?
Python lists are mutable because they store references to their elements, instead of the elements themselves. Which means, the list variable would have a different address than the address of its ...
1
vote
1
answer
69
views
Depth first search: is immutabilty and speed mutualy exclusive?
The DFS I was taught in school goes something like this:
(* graph representation: ith element of the array is a list of successors of the node i *)
let graph_example = [|
[1; 2];
[3; 0; 2];
[0; ...
2
votes
1
answer
91
views
Can I declare a constructor that accepts Name-Value arguments to define immutable properties?
Since MATLAB R2019b it has been possible to declare name-value arguments from class properties which has created an elegant way to define a class that can be constructed with a declaration of its ...
1
vote
0
answers
111
views
Persistent data structures in Rust
How to implementing a basic persistent data structure that utilizes structural sharing? The code provided below is functional, but I would like to receive input on whether this approach is suitable ...
0
votes
1
answer
98
views
Mutability in Rust: value borrowed after move?
I've been writing a small process manager that monitors the state of a few spawned processes and aims to keep them up whenever they get killed. Unfortunately, I seem to be facing mutability issues ...
-1
votes
1
answer
41
views
Double linked immutable objects (C# records)
I want to create two-way linked objects (parent with child, where Parent.Child property refers to the parent's child, and Child.Parent refers to the child's parent).
From what I see, this would only ...
2
votes
2
answers
68
views
Array passed as prop doesn't reflect changes
This question is more for understanding more how react handles and reacts to changes, than implementation, therefore I'm letting immutable-props-apprach go for a little bit.
I'm trying to get the ...
0
votes
0
answers
56
views
Spread operator vs filter in React/immutable data?
Possibly related to Array.from() vs spread syntax
This is just a theoretical question, in reality we wouldn't have a such huge json dumped into the client side.
Let's say we have a object such that
{
...
1
vote
2
answers
409
views
How to use late final keyword in Flutter Dart freezed model?
How to use late final keyword in Flutter Dart freezed model ?
This code generates successfully and has no static analysis error but it does not compile strangely.
import 'dart:convert';
import '...
0
votes
0
answers
145
views
Dart/Flutter - final unmodifiable list field
My app uses Flutter Bloc to manage app state. My state clas is defined as follows:
@immutable
@CopyWith()
class MyState extends Equatable {
const ReservationsState({
this.list1= const [],
...
1
vote
3
answers
95
views
How to do this in Kotlin?
I am trying to create a simple data class for a country.
data class Country(val name: String, val capital: String)
This works fine. Now I want to add neighbors.
data class Country(val name: String, ...