immutability

Immutable class vs struct

邮差的信 提交于 2019-12-03 09:28:56
问题 The following are the only ways classes are different from structs in C# (please correct me if I'm wrong): Class variables are references, while struct variables are values, therefore the entire value of struct is copied in assignments and parameter passes Class variables are pointers stored on stack that point to the memory on heap, while struct variables are on stored heap as values Suppose I have an immutable struct, that is struct with fields that cannot be modified once initialized. Each

Update one of the objects in array, in an immutable way

戏子无情 提交于 2019-12-03 08:48:15
问题 In React's this.state I have a property called formErrors containing the following dynamic array of objects. [ {fieldName: 'title', valid: false}, {fieldName: 'description', valid: true}, {fieldName: 'cityId', valid: false}, {fieldName: 'hostDescription', valid: false}, ] Let's say I would need to update state's object having the fieldName cityId to the valid value of true . What's the easiest or most common way to solve this? I'm OK to use any of the libraries immutability-helper, immutable

Why tuple is not mutable in Python? [duplicate]

安稳与你 提交于 2019-12-03 08:30:40
This question already has answers here : Why are python strings and tuples are made immutable? (6 answers) Possible Duplicate: Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why this feature is useful? A few reasons: Mutable objects like lists cannot be used as dictionary keys or set members in Python, since they are not hashable . If lists were given __hash__ methods based on their contents, the values returned could change as the contents change, which violates the contract for hash values. If Python only had mutable sequences

Immutable data structures performance

偶尔善良 提交于 2019-12-03 07:27:47
问题 I don't get how can something as a Set be immutable and still have an acceptable performance. From what I've read in F# Sets internally use Red Black Trees as their implementation. If each time we want to add something new to a Red Black Tree we have to basically recreate it, how can it have ever good performance? What am I missing here? Although I am asking this for F#'s Sets, I think this is as relevant in any other language which has or uses immutable data structures. Thanks 回答1: Almost

Why String is immutable or final in Java [duplicate]

我是研究僧i 提交于 2019-12-03 07:14:02
This question already has answers here : Why is the String class declared final in Java? (16 answers) As i was told this is important String Interview question in Java, which starts with discussion of " What is String ", how String is different in java than in C or C++ and then you are asked about immutable objects and you're asked the main question: " Why String is immutable or final in Java ". Can you share your Ideas ? Thanks in advance. It is mainly for security reasons . String is used as parameter in network connection, database url etc. It can be easily attacked if it is mutable

Creating an immutable object without final fields?

匆匆过客 提交于 2019-12-03 06:51:35
Can we create an immutable object without having all fields final? If possible a couple of examples would be helpful. Declare all fields private and only define getters: public final class Private{ private int a; private int b; public int getA(){return this.a;} public int getB(){return this.b;} } citing @Jon Skeet's comment, final class modifier is useful for: While an instance of just Private is immutable, an instance of a subclass may well be mutable. So code receiving a reference of type Private can't rely on it being immutable without checking that it's an instance of just Private. So if

Optionally adding items to a Scala Map

余生长醉 提交于 2019-12-03 06:40:35
问题 I am looking for an idiomatic solution to this problem. I am building a val Scala (immutable) Map and would like to optionally add one or more items: val aMap = Map(key1 -> value1, key2 -> value2, (if (condition) (key3 -> value3) else ???)) How can this be done without using a var ? What should replace the ??? ? Is it better to use the + operator? val aMap = Map(key1 -> value1, key2 -> value2) + (if (condition) (key3 -> value3) else ???)) One possible solution is: val aMap = Map(key1 ->

Examples of Immutable Types in .Net

空扰寡人 提交于 2019-12-03 06:31:05
We know the concept of immutability but need to know few immutable types other than String DateTime Are there more? stakx A list of immutable types in the framework class library follows below. (Feel free to expand it!) System.… All primitive value types: (Note: not all value types are immutable !) Byte and SByte Int16 and UInt16 Int32 and UInt32 Int64 and UInt64 IntPtr Single Double Decimal All anonymous types created by the compiler ( new { ... } in C#, New With { ... } in VB.NET) (Wrong for two reasons: These types are not in the FCL, and apparently VB.NET types are mutable.) All

Java - Immutable array thread-safety

混江龙づ霸主 提交于 2019-12-03 06:21:36
I have a question regarding the Java Memory Model. Here is a simple class presenting the problem: public class ImmutableIntArray { private final int[] array; public ImmutableIntArray() { array = new int[10]; for (int i = 0; i < 10; i++) { array[i] = i; } } // Will always return the correct value? public int get(int index) { return array[index]; } } As far as I know the JMM guarantees that the value of final fields will be visible to other threads after construction. But I want to ensure that other threads will see the most recent version of data stored in the array after construction. Of

Why won't declaring an array final make it immutable in Java?

流过昼夜 提交于 2019-12-03 06:09:26
Why won't declaring an array final make it immutable in Java? Doesn't declaring something final mean it can't be changed? From question related to immutable array it's clear that declaring an array final doesn't make it unchangeable. The following is possible. final int[] array = new int[] {0, 1, 2, 3}; array[0] = 42; My question is: What is the function of declaring final here then? final is only about the reference that is marked by it; there is no such thing as an immutable array in Java. When you say private final int[] xs = new int[20]; you won't be allowed to say xs = new int[10]; later