immutability

What is the purpose of modifying a string using reflection?

ぐ巨炮叔叔 提交于 2019-12-01 06:01:27
问题 I was reading an article that said that Java strings are not completely immutable. However, in the article's sample code that modifies the string, it makes a call to string.toUpperCase().toCharArray(), which returns a new string. So what's the purpose of going through the process of changing the string if you call toUpperCase() anyway? Here is the code: public static void toUpperCase(String orig) { try { Field stringValue = String.class.getDeclaredField("value"); stringValue.setAccessible

Why instance variable to be final?

↘锁芯ラ 提交于 2019-12-01 05:50:29
I read this question about immutable objects and was left with a question regarding immutable objects and final field: Why do we need instance variable in immutable class to be final? For example, consider this immutable class: public final class Immutable { private final int someVal; public Immutable(int someVal) { this.someVal= someVal; } public int getVal() { return val; } } If in the above code there is no set methods and the instance variable is set only within the constructor, why is it required that the instance variable is declared final? There is no requirement as such to make the

Why do most classes of the HttpClient API define immutable objects?

瘦欲@ 提交于 2019-12-01 05:42:19
问题 The documentation for HttpClient states the following about immutability: Interceptors exist to examine and mutate outgoing requests and incoming responses. However, it may be surprising to learn that the HttpRequest and HttpResponse classes are largely immutable. This is for a reason: because the app may retry requests, the interceptor chain may process an individual request multiple times. If requests were mutable, a retried request would be different than the original request. Immutability

Are Delphi strings immutable?

▼魔方 西西 提交于 2019-12-01 05:12:26
As far as I know, strings are immutable in Delphi. I kind of understand that means if you do: string1 := 'Hello'; string1 := string1 + " World"; first string is destroyed and you get a reference to a new string "Hello World". But what happens if you have the same string in different places around your code? I have a string hash assigned for identifying several variables, so for example a "change" is identified by a hash value of the properties of that change. That way it's easy for me to check to "changes" for equality. Now, each hash is computed separately (not all the properties are taken

Immutable readonly reference types & FXCop Violation: Do not declare read only mutable reference types

淺唱寂寞╮ 提交于 2019-12-01 04:51:51
I have been trying to wrap my head around this FXCop violation "DoNotDeclareReadOnlyMutableReferenceTypes" MSDN: http://msdn.microsoft.com/en-us/library/ms182302%28VS.80%29.aspx Code from MSDN which would cause this violation: namespace SecurityLibrary { public class MutableReferenceTypes { static protected readonly StringBuilder SomeStringBuilder; static MutableReferenceTypes() { SomeStringBuilder = new StringBuilder(); } } } From Jon's answer here and here , I understand that the field holding the reference to the object (in this case SomeStringBuilder) is readonly and not the object itself

Is there a bidirectional multimap persistent data structure?

谁说胖子不能爱 提交于 2019-12-01 04:09:51
In other words, can we model many to many relationships in a persistent data structure efficiently? A pair of unidirectional multimaps was suggested. However, I'm not sure how this would work well for removal in a persistent data structure. Let's take the case where we have keys 1..4 to values "1".."4" and let's say they each refer to all the others, so we have two maps that look very similar for both directions: {1 => ["2","3","4"], 2 => ["1","3","4"], ...} {"1" => [2,3,4], "2" => [1,3,4], ...} Now we want to remove item 1 completely from the system. That requires changing one node in the

What is the advantage of annotating an immutable Java class with @Immutable?

吃可爱长大的小学妹 提交于 2019-12-01 03:46:11
I get the concept of immutability, and why it is a good idea to make DTOs immutable. I also notice that Java has an @Immutable annotation that we can use to annotate immutable classes. My question is: what does annotating a Java class as @Immutable give us? Are there any library features that only work on classes annotated in this way? The annotation documents the fact that your class is immutable and tells the users of the class that you have followed the contract defined in the annotation javadoc . It is also frequent to simply include a comment directly in the javadoc: this is the approach

How to get an arbitrary element from a frozenset?

坚强是说给别人听的谎言 提交于 2019-12-01 03:22:09
I would like to get an element from a frozenset (without modifying it, of course, as frozenset s are immutable). The best solution I have found so far is: s = frozenset(['a']) iter(s).next() which returns, as expected: 'a' In other words, is there any way of 'popping' an element from a frozenset without actually popping it? (Summarizing the answers given in the comments) Your method is as good as any, with the caveat that, from Python 2.6, you should be using next(iter(s)) rather than iter(s).next() . If you want a random element rather than an arbitrary one, use the following: import random

Is Dictionary broken or should GetHashCode() only base on immutable members?

允我心安 提交于 2019-12-01 03:01:54
When an object is added to the .NET System.Collections.Generic.Dictionary class the hashcode of the key is stored internally and used for later comparisons. When the hashcode changes after its initial insertion into the dictionary it often becomes "inaccessible" and may surprise its users when an existence check, even using the same reference, returns false (sample code below). The GetHashCode documentation says: The GetHashCode method for an object must consistently return the same hash code as long as there is no modification to the object state that determines the return value of the object

Why continue to use getters with immutable objects?

断了今生、忘了曾经 提交于 2019-12-01 02:31:04
Using immutable objects has become more and more common, even when the program at hand is never meant to be ran in parallel. And yet we still use getters, which require 3 lines of boilerplate for every field and 5 extra characters on every access (in your favorite mainstream OO language). While this may seem trivial, and many editors remove most of the burden from the programmer anyways, it is still seemingly unnecessary effort. What are the reasons for the continued use of accessors versus direct field access of immutable objects? Specifically, are there advantages to forcing the user to use