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
4 answers

Passing and storing pointers to immutable types and strings in C#

Is there a way to store a pointer to immutable types like strings in C#? How can execute: Instance1.SomeFunction(out MyString); ,and store a pointer to MyString inside of Instance1?
AareP
  • 2,355
  • 3
  • 21
  • 28
2
votes
1 answer

How to freeze an array in swift?

In js I can freeze an array after adding some elements to an array. Is there anything to freeze an array in Swift? What is freezing? Ans: Suppose we have an array. We add some elements to that array. /* This is javascript code */ var fruits =…
Faruk Hossain
  • 1,205
  • 5
  • 12
2
votes
2 answers

What are the drawbacks in returning empty immutable collection instead of allocating a new and empty one?

I used to call for Collections.emptyList(), emptySet and emptyMap to return a reference to an immutable empty collection instead of null from my functions. That's because I see no reasons to allocate a new empty collection instance: private static…
Kirill
  • 6,762
  • 4
  • 51
  • 81
2
votes
1 answer

Cannot access properties of an object, but the object exists

I was trying to sort an array of user objects coming back from the database base on their age, but something really strange happened. The following code isn't working: async function getAllUsers(){ let _users = await User.find({}); …
kjunz
  • 111
  • 1
  • 7
2
votes
8 answers

Is this class Threadsafe?

Hi Is the class below threadsafe? class ImmutablePossiblyThreadsafeClass { private final Map map; public ImmutablePossiblyThreadsafeClass(final Map map) { this.map = new HashMap(); for (Entry
user63904
2
votes
1 answer

How to initialize final member with other final member in StatelessWidget subclass?

I have a simple StatelessWidget that creates a new StatefulWidget in its constructor. The problem is that to create that widget, another object needs to be created first. And then when I pass it to the constructor, I get the error error: Only…
UglyBob
  • 247
  • 1
  • 14
2
votes
0 answers

Vuex and the mutate thing, the immutability and the clone deep hell

This is not a questions itself, but a good issue to Vue community. I want to open a discussion about Vuex and immutability. When we enable Vuex strict mode and when we have within the state an object called by a computed function that we need to…
calebeaires
  • 1,972
  • 1
  • 22
  • 34
2
votes
1 answer

Spark Shell allowing to redeclare the same immutable variable

I am working with Spark-shell for Scala and found a strange behaviour in Spark-shell REPL which is not there if i use any IDE. I can declare the same immutable variable again and again in REPL, but the same is not allowed in IDE. Here is the code in…
KayV
  • 12,987
  • 11
  • 98
  • 148
2
votes
1 answer

React: How to modify an array-based state, without an index or a unique identifier?

Let's say i have a tabbed component. Each tab presents a person object. The user can switch the active tab, modify or remove each one and even change their order: state={ activeTab:0, tabs:[ { name:'John', age:34 }, { …
i.brod
  • 3,993
  • 11
  • 38
  • 74
2
votes
0 answers

Immutability and components props in React native (vs. React)

This question is somewhat related to How to pass props to {this.props.children}. That may sound like a dumb question which has been answered a million times, but I can't figure it out what is true and what is not from what I've read so far. I can't…
cglacet
  • 8,873
  • 4
  • 45
  • 60
2
votes
2 answers

Resize 2d array immutably

Demo: https://jsfiddle.net/yxm9d4L0/2/ Given a 2D 3x3 array: let arr = [ [null, null, null], [null, {'hello': 'world'}, null], [null, null, null] ] and new sizes greater than current length: width = 10; height = 8; or less than to…
alt-rock
  • 347
  • 1
  • 6
  • 18
2
votes
1 answer

Can we rely on Gson's ability to modify val attributes?

Kotlin's val attributes, by design are immutable. They are supposed to be fixed and unchangeable after initialization. However, I accidentally found out that Gson is able to modify those attributes. Consider this example: //Entity class: class User…
Mousa
  • 2,190
  • 3
  • 21
  • 34
2
votes
1 answer

Using an ImmutableJS Object as the React Component State

I am trying to use ImmutableJS to help maintain state in a React component. I can use it just fine if the state contains an immutable object. But, if the entire state is an immutable object, then it fails whenever I try to setState() with an error…
rdavis
  • 143
  • 1
  • 1
  • 5
2
votes
2 answers

Data descriptors for python module

I know you can define data descriptors for a class instance using the __get__ and __set__ methods. Is it possible to define something similar for an imported module ? Use case: I have a large test file with a lot of dictionaries defined in a…
Tobey
  • 1,400
  • 1
  • 10
  • 25
2
votes
1 answer

States aren't being updated

I'm currently working on an image uploader component in React. Everything works fine but the deleting method. I've read a couple of articles on how to update arrays/objects and the idea of immutable state. Here's what I've…
Brian Le
  • 2,646
  • 18
  • 28
1 2 3
99
100