immutability

How to serialize / deserialize immutable list type in c#

时光总嘲笑我的痴心妄想 提交于 2019-11-28 00:51:20
问题 If I have a class defined [DataContract()] class MyObject { [DataMember()] ImmutableList<string> Strings { get; private set} } The ImmutableList<T> type comes from the immutables library https://www.nuget.org/packages/Microsoft.Bcl.Immutable. Note that the class ImmutableList does not have a default constructor or a mutable Add method. Adding things to the list take the form. myList = myList.Add("new string"); Can I add some custom support to the .NET serialization mechanism to support this

Will the jit optimize new objects

匆匆过客 提交于 2019-11-28 00:25:52
I created this class for being immutable and having a fluent API: public final class Message { public final String email; public final String escalationEmail; public final String assignee; public final String conversationId; public final String subject; public final String userId; public Message(String email, String escalationEmail, String assignee, String conversationId, String subject, String userId) { this.email = email; this.escalationEmail = escalationEmail; this.assignee = assignee; this.conversationId = conversationId; this.subject = subject; this.userId = userId; } public Message() {

effect of changing String using reflection

吃可爱长大的小学妹 提交于 2019-11-27 23:40:33
As we all know, String is immutable in java. however, one can change it using reflection, by getting the Field and setting access level. (I know it is unadvised, I am not planning to do so, this question is pure theoretical). my question: assuming I know what I am doing (and modify all fields as needed), will the program run properly? or does the jvm makes some optimizations that rely on String being immutable? will I suffer performance loss? if so, what assumption does it make? what will go wrong in the program p.s. String is just an example, I am interested actually in a general answer, in

Efficient, Immutable, Extensible Collections for .NET [duplicate]

橙三吉。 提交于 2019-11-27 22:56:12
This question already has an answer here: Immutable collections? 10 answers It seems to me there is an extreme lack of safe, immutable collection types for .NET, in particular BCL but I've not seen much work done outside either. Do anyone have any pointers to a (preferably) production quality, fast, immutable collections library for .NET. A fast list type is essential. I'm not yet prepared to switch to F#. *Edit: Note to searchers, this is being rolled into the BCL soon: .NET immutable collections You might want to take a look at the Microsoft.FSharp.Collections namespace in the FSharp.Core

In C#, why can't I modify the member of a value type instance in a foreach loop?

岁酱吖の 提交于 2019-11-27 20:21:10
I know that value types should be immutable, but that's just a suggestion, not a rule, right? So why can't I do something like this: struct MyStruct { public string Name { get; set; } } public class Program { static void Main(string[] args) { MyStruct[] array = new MyStruct[] { new MyStruct { Name = "1" }, new MyStruct { Name = "2" } }; foreach (var item in array) { item.Name = "3"; } //for (int i = 0; i < array.Length; i++) //{ // array[i].Name = "3"; //} Console.ReadLine(); } } The foreach loop in the code doesn't compile while the commented for loop works fine. The error message: Cannot

Functional programming: state vs. reassignment

元气小坏坏 提交于 2019-11-27 20:09:11
问题 I need help getting my head around the difference between my current OOP notion of state, and the way it would be done in a functional language like Haskell or Clojure. To use a hackneyed example, let's say we're dealing with simplified bank account objects/structs/whatever. In an OOP language, I'd have some class holding a reference to a BankAccount, which would have instance variables for things like interest rate, and methods like setInterestRate() which change the object's state and

DeepReadonly Object Typescript

穿精又带淫゛_ 提交于 2019-11-27 19:22:13
问题 It is possible to create a DeepReadonly type like this: type DeepReadonly<T> = { readonly [P in keyof T]: DeepReadonly<T[P]>; }; interface A { B: { C: number; }; D: { E: number; }[]; } const myDeepReadonlyObject: DeepReadonly<A> = { B: { C: 1 }, D: [ { E: 2 } ], } myDeepReadonlyObject.B = { C: 2 }; // error :) myDeepReadonlyObject.B.C = 2; // error :) This is great. Both B and B.C are readonly. When I try to modify D however... // I'd like this to be an error myDeepReadonlyObject.D[0] = { E:

Why are immutable objects thread-safe?

こ雲淡風輕ζ 提交于 2019-11-27 19:19:59
class Unit { private readonly string name; private readonly double scale; public Unit(string name, double scale) { this.name = name; this.scale = scale, } public string Name { get { return name; } } public string Scale { get { return scale; } } private static Unit gram = new Unit("Gram", 1.0); public Unit Gram { get { return gram; } } } Multiple threads have access to Unit.Gram . Why is it ok for multiple threads simultaneously read Unit.Gram.Title ? My concern is that they are referring to the same memory location. One thread starts reading that memory, so isn't it "locked out" then? Does the

Idiomatic Way to declare C++ Immutable Classes

假如想象 提交于 2019-11-27 19:19:18
So I have some pretty extensive functional code where the main data type is immutable structs/classes. The way I have been declaring immutability is "practically immutable" by making member variables and any methods const. struct RockSolid { const float x; const float y; float MakeHarderConcrete() const { return x + y; } } Is this actually the way "we should do it" in C++? Or is there a better way? The way you proposed is perfectly fine, except if in your code you need to make assignment of RockSolid variables, like this: RockSolid a(0,1); RockSolid b(0,1); a = b; This would not work as the

Why did Matz choose to make Strings mutable by default in Ruby?

丶灬走出姿态 提交于 2019-11-27 19:15:56
It's the reverse of this question: Why can't strings be mutable in Java and .NET? Was this choice made in Ruby only because operations (appends and such) are efficient on mutable strings, or was there some other reason? (If it's only efficiency, that would seem peculiar, since the design of Ruby seems otherwise to not put a high premium on faciliating efficient implementation.) This is in line with Ruby's design, as you note. Immutable strings are more efficient than mutable strings - less copying, as strings are re-used - but make work harder for the programmer. It is intuitive to see strings