immutability

Why does Microsoft advise against readonly fields with mutable values?

谁说我不能喝 提交于 2019-11-30 02:44:31
In the Design Guidelines for Developing Class Libraries , Microsoft say: Do not assign instances of mutable types to read-only fields. The objects created using a mutable type can be modified after they are created. For example, arrays and most collections are mutable types while Int32, Uri, and String are immutable types. For fields that hold a mutable reference type, the read-only modifier prevents the field value from being overwritten but does not protect the mutable type from modification. This simply restates the behaviour of readonly without explaining why it's bad to use readonly. The

pre-release Collections.Immutable

我怕爱的太早我们不能终老 提交于 2019-11-30 01:47:10
问题 Has anyone succeeded in opening the pre-release System.Collections.Immutable from NuGet in F#? I'm getting this error: The type 'IEnumerable`1' is required here and is unavailable. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Even though my project is .NET 4.5 Tried explicitly opening System.Runtime and that did not work either. I am referencing .NETCore\v4.5\System.Runtime.dll 回答1: This is a known issue that the

How/why does set() in {frozenset()} work?

≡放荡痞女 提交于 2019-11-30 01:11:12
问题 Even though sets are unhashable, membership check in other set works: >>> set() in {frozenset()} True I expected TypeError: unhashable type: 'set' , consistent with other behaviours in Python: >>> set() in {} # doesn't work when checking in dict TypeError: unhashable type: 'set' >>> {} in {frozenset()} # looking up some other unhashable type doesn't work TypeError: unhashable type: 'dict' So, how is set membership in other set implemented? 回答1: The last line of the documentation for sets

Immutability and XML Serialization

亡梦爱人 提交于 2019-11-29 22:56:37
I have several classes that are immutable once their initial values are set. Eric Lippert calls this write-once immutability . Implementing write-once immutability in C# usually means setting the initial values via the constructor. These values initialize readonly fields. But if you need to serialize a class like this to XML, using either the XmlSerializer or the DataContractSerializer, you must have a parameterless constructor. Does anyone have suggestions for how to work around this problem? Are there other forms of immutability that work better with serialization? EDIT: As @Todd pointed out

How to create immutable objects in C#?

久未见 提交于 2019-11-29 22:13:07
In a question about Best practices for C# pattern validation , the highest voted answer says: I tend to perform all of my validation in the constructor. This is a must because I almost always create immutable objects. How exactly do you create an immutable object in C#? Do you just use the readonly keyword? How exactly would this work if you want to validate in the constructor of your Entity Framework generated model class? Would it look like below? public partial readonly Person { public Person() } The interesting question here is your question from the comments: What kind of object would you

How do I work around mutability in moment.js?

╄→гoц情女王★ 提交于 2019-11-29 21:59:29
I've run into a problem where I have to store the initial values of a moment object but I'm having some trouble preventing my variable from changing along with the original object. Unfortunately Object.freeze() doesn't work, because moment.js returns an "Invalid date" error when I try to format that. There's a Moment.js plugin on NPM called frozen-moment - You could use moment().freeze() in place of Object.freeze(moment()) . Otherwise, vanilla Moment.js has a clone method that should help you avoid mutability problems, so you could do something like this: var a = moment(), b = a.clone(); // or

Why is shared mutability bad?

£可爱£侵袭症+ 提交于 2019-11-29 20:45:29
I was watching a presentation on Java, and at one point, the lecturer said: "Mutability is OK, sharing is nice, shared mutability is devil's work." What he was referring to is the following piece of code, which he considered an "extremely bad habit": //double the even values and put that into a list. List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 1, 2, 3, 4, 5); List<Integer> doubleOfEven = new ArrayList<>(); numbers.stream() .filter(e -> e % 2 == 0) .map(e -> e * 2) .forEach(e -> doubleOfEven.add(e)); He then proceeded writing the code that should be used, which is: List<Integer>

Enums in Javascript with ES6

久未见 提交于 2019-11-29 19:13:34
I'm rebuilding an old Java project in Javascript, and realized that there's no good way to do enums in JS. The best I can come up with is: const Colors = { RED: Symbol("red"), BLUE: Symbol("blue"), GREEN: Symbol("green") }; Object.freeze(Colors); The const keeps Colors from being reassigned, and freezing it prevents mutating the keys and values. I'm using Symbols so that Colors.RED is not equal to 0 , or anything else besides itself. Is there a problem with this formulation? Is there a better way? (I know this question is a bit of a repeat, but all the previous Q/As are quite old, and ES6

Why are immutable objecs loved by JVM's GC?

青春壹個敷衍的年華 提交于 2019-11-29 18:46:41
问题 I know the reason that JVM GC loves short-live object because it can be collected in minor GC. But why does JVM GC love immutable objects? EDIT: Charlie Hunt says that GC loves immutable objects in his presentation. Thanks 回答1: If the GC can know that an object doesn't contain any references to any gen0 objects, then it can be ignored when performing a gen0 collection. Likewise if an object doesn't contain any reference to any gen0 or gen1 objects, it may be ignored when performing a gen1

How can I force a struct's field to always be immutable in Rust?

泄露秘密 提交于 2019-11-29 18:26:48
问题 In Rust, you don't specify mutability inside a struct , but it is inherited from the variable binding. That's great, but is it possible to force a field to be always immutable, even when the root is mutable? Something like this hypothetical syntax: struct A { immut s: Shape, // immutable by design bla: Bla, // this field inheriting (im)mutability } let mut a = make_a(); a.s = x/*...*/; // illegal This would help to maintain nice semantic restrictions in a program, just like Java's final does