immutability

Empirical data on the effects of immutability?

可紊 提交于 2019-11-29 17:33:11
问题 In class today, my professor was discussing how to structure a class. The course primarily uses Java and I have more Java experience than the teacher (he comes from a C++ background), so I mentioned that in Java one should favor immutability. My professor asked me to justify my answer, and I gave the reasons that I've heard from the Java community: Safety (especially with threading) Reduced object count Allows certain optimizations (especially for garbage collector) The professor challenged

Immutable Dictionary Vs Dictionary Vs C5 Vs F# - performance

谁说胖子不能爱 提交于 2019-11-29 17:25:35
问题 Our application uses plenty of dictionaries which have multi level lookup that are not frequently changing. We are investigating at converting some of the critical code that does a lot of lookup using dictionaries to use alternate structures for - faster lookup, light on the memory/gc. This made us compare various dictionaries/libraries available - Dictionary ( System.Collections.Generics.Dictionary -SCGD), ImmutableDictionary , C5.HashDictionary , FSharpMap . Running the following program

Does StringBuilder become immutable after a call to ToString?

风流意气都作罢 提交于 2019-11-29 17:00:38
问题 I distinctly remember from the early days of .NET that calling ToString on a StringBuilder used to provide the new string object (to be returned) with the internal char buffer used by StringBuilder. This way if you constructed a huge string using StringBuilder, calling ToString didn't have to copy it. In doing that, the StringBuilder had to prevent any additional changes to the buffer, because it was now used by an immutable string. As a result the StringBuilder would switch to a "copy-on

Why are System.Windows.Point & System.Windows.Vector mutable?

限于喜欢 提交于 2019-11-29 16:59:36
问题 Given that mutable structs are generally regarded as evil (e.g., Why are mutable structs “evil”?), are there potential benefits that might have prompted the designers of the .NET framework to make System.Windows.Point & System.Windows.Vector mutable? I'd like to understand this so I can decide whether it would make sense to make my own similar structs mutable (if ever). It's possible the decision to make Point and Vector mutable was just an error in judgment, but if there was a good reason (e

Ways to make a class immutable in Python

折月煮酒 提交于 2019-11-29 16:54:52
问题 I'm doing some distributed computing in which several machines communicate under the assumption that they all have identical versions of various classes. Thus, it seems to be good design to make these classes immutable; not in the sense that it must thwart a user with bad intentions, just immutable enough that it is never modified by accident. How would I go about this? For example, how would I implement a metaclass that makes the class using it immutable after it's definition? >>> class A

Polymorphic updates in an immutable class hierarchy

不想你离开。 提交于 2019-11-29 16:46:07
问题 I'd like to be able to assemble domain objects from traits, according to various properties that the concrete classes might have. When my objects are mutable, this is pretty straightforward. For example: trait HasHitPoints { var hitPoints: Int = 100 } trait HasBearing { var bearing: Double = 0 } class Ship extends HasHitPoints with HasBearing class Base extends HasHitPoints val entities = new Ship :: new Base :: Nil entities.collect { case h: HasHitPoints => h.hitPoints += 10 } In particular,

Immutable objects and unmodifiable collections

喜夏-厌秋 提交于 2019-11-29 15:43:20
With an immutable object, where is it proper to wrap a contained collection as unmodifiable? I see 3 options: In the immutable object's factory: public class ImmutableFactory { public Immutable build(){ List<Integer> values = new ArrayList<Integer>(); values.add(1); values.add(2); values.add(3); return new Immutable(Collections.unmodifiableList(values), "hello"); } } In the immutable's contructor public class Immutable { private final List<Integer> values; private final String hello; public Immutable(List<Integer> values, String hello) { this.values = Collections.unmodifiableList(values); this

making a class immutable in java

*爱你&永不变心* 提交于 2019-11-29 14:05:33
To make a class immutable what I can do is: 1)Make class final 2)do not provide setters 3)mark all variables as final But if my class has another object of some other class then , somone can change value of that object class MyClass{ final int a; final OtherClass other MyClass(int a ,OtherClass other){ this.a = a; this.other = other; } int getA(){ return a; } OtherClass getOther(){ return other; } public static void main(String ags[]){ MyClass m = new Myclass(1,new OtherClass); Other o = m.getOther(); o.setSomething(xyz) ; //This is the problem ,How to prevent this? } } A) Make the OtherClass

Mutability of string when string doesn't change in C#

和自甴很熟 提交于 2019-11-29 12:56:38
If the string operation doesn't change the value of string, will that end up in creation of a new instance? For example, string str = "foo"; str += ""; I know the difference between string and StringBuilder in C#. Shamseer K No, a new instance will be created only when the string operation changes the value in the string variable. It can be proved using the ObjectIDGenerator class. It's worth reading this complete article for proof. using System; using System.Text; using System.Runtime.Serialization; namespace StringVsStringBuilder { class Program { static void Main(string[] args) {

Use of guava immutable collection as method parameter and/or return type

孤街浪徒 提交于 2019-11-29 12:24:10
问题 I am trying to determine what the best practices would be for an ImmutableList. Below is a simplistic example that will help drive my questions: Ex: public ImmutableCollection<Foo> getFooOne(ImmutableList<Foo> fooInput){ //.. do some work ImmutableList<Foo> fooOther = // something generated during the code return fooOther; } public Collection<Foo> getFooTwo(List<Foo> fooInput){ //.. do some work List<Foo> fooOther = // something generated during the code return ImmutableList.copyOf(fooOther);