immutability

Why is Font immutable?

泄露秘密 提交于 2019-12-05 00:39:30
Font being immutable distresses both the programmer and the GC, because you need to create a new instance every time. Why is Font an immutable reference type then? It simplifies the usage from the render system. If the framework were to allow Font to be mutable, it would need to detect changes, and rework how it's rendering happens on a regular basis. Since Font creates a native resource, keeping this immutable prevents the system from worrying about having to recreate handles internally on a repeated basis. Also, I disagree in terms of "Distress to the programmer". By making Font immutable,

Do not declare read only mutable reference types - why not?

偶尔善良 提交于 2019-12-05 00:15:06
I have been reading this question and a few other answers and whilst I get the difference between changing the reference and changing the state of the current instance I'm not certain why this means that I shouldn't mark it readonly. Is this because marking something as readonly tells the compiler something special about the instance and so it is able to then treat it as thread safe when it actually might not be? Presumably there are situations where I don't want the instance to be able to be changed, but don't mind if the state of the instance is changed (singleton maybe. /me prepares for

Normalized and immutable data model

一个人想着一个人 提交于 2019-12-04 23:53:04
How does Haskell solve the "normalized immutable data structure" problem? For example, let's consider a data structure representing ex girlfriends/boyfriends: data Man = Man {name ::String, exes::[Woman]} data Woman = Woman {name :: String, exes::[Man]} What happens if a Woman changes her name and she had been with 13 man? Then all the 13 man should be "updated" (in the Haskell sense) too? Some kind of normalization is needed to avoid these "updates". This is a very simple example, but imagine a model with 20 entities, and arbitrary relationships between them, what to do then? What is the

Does String.Replace() create a new string if there's nothing to replace?

两盒软妹~` 提交于 2019-12-04 22:53:55
For example: public string ReplaceXYZ(string text) { string replacedText = text; replacedText = replacedText.Replace("X", String.Empty); replacedText = replacedText.Replace("Y", String.Empty); replacedText = replacedText.Replace("Z", String.Empty); return replacedText; } If I were to call "ReplaceXYZ" even for strings that do not contain "X", "Y", or "Z", would 3 new strings be created each time? I spotted code similar to this in one of our projects. It's called repeatedly as it loops through a large collection of strings. It does not return a new instance if there is nothing to replace:

ReadOnlyCollection vs Liskov - How to correctly model immutable representations of a mutable collection

試著忘記壹切 提交于 2019-12-04 22:24:53
Liskov-substitution principle requires that subtypes must satisfy the contracts of super-types. In my understanding, this would entail that ReadOnlyCollection<T> violates Liskov. ICollection<T> 's contract exposes Add and Remove operations, but the read only subtype does not satisfy this contract. For example, IList<object> collection = new List<object>(); collection = new System.Collections.ObjectModel.ReadOnlyCollection<object>(collection); collection.Add(new object()); -- not supported exception There is clearly a need for immutable collections. Is there something broken about .NET's way of

Java Immutable Queue

巧了我就是萌 提交于 2019-12-04 22:22:58
In the code below I have two linked lists liperm and litemp. I want to initialize litemp first with the values of liperm and then add other values. But it is not working as in it is not initializing them. Can you please help: public class ExamImmutableQueueImpl<E> implements ExamImmutableQueue<E> { LinkedList<E> liperm = new LinkedList<E>(); LinkedList<E> litemp = new LinkedList<E>(liperm); public ExamImmutableQueueImpl(LinkedList li){ System.out.println(li.toString()); } public ExamImmutableQueueImpl(){} @Override public ExamImmutableQueue<E> enqueue(E e) { System.out.println(litemp.toString(

Can an immutable type change its internal state?

。_饼干妹妹 提交于 2019-12-04 22:12:35
The question is simple. Can a type that can change its internal state without it being observable from the outside be considered immutable ? Simplified example: public struct Matrix { bool determinantEvaluated; double determinant; public double Determinant { get //asume thread-safe correctness in implementation of the getter { if (!determinantEvaluated) { determinant = getDeterminant(this); determinantEvaluated = true; } return determinant; } } } UPDATE : Clarified the thread-safeness issue, as it was causing distraction. Yes, immutable can change its state, providing that the changes are

Scala “update” immutable object best practices

↘锁芯ラ 提交于 2019-12-04 21:58:07
问题 With a mutable object I can write something like var user = DAO.getUser(id) user.name = "John" user.email ="john@doe.com" // logic on user If user is immutable then I need to clone\copy it on every change operation. I know a few ways to perform this case class copy method (like changeName) that creates a new object with the new property What is the best practice? And one more question. Is there any existing technique to get "changes" relative to the original object(for example to generate

Immutability in Hyperledger Fabric

狂风中的少年 提交于 2019-12-04 21:44:01
Can someone explain how the immutability is implemented in Hyperledger Fabric? If we have private channel with little amount of peers, how it can be guaranteed, that one side hasn't changed data in it's ledger? In order to guarantee that no party in the channel has tampered data in its own favor you need to present sophisticated endorsement policy to include all required parties and make sure they adequately represented within endorsement policy. Hence making it obligatory for client which issues new transaction to get endorsement from all interested parties, hence ensuring that all have same

How to create default constructor for immutable class

回眸只為那壹抹淺笑 提交于 2019-12-04 20:31:57
问题 I like to make my objects immutable based on this article (Why objects must be immutable). However, I am trying to parse an object using Jackson Object Mapper. I was initially getting JsonMappingException: No suitable constructor found for type [simple type, class ]: cannot instantiate from JSON object. I could fix it as mentioned here, by providing a default constructor and making my fields non-final. import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; import