immutable

LinkedList insert tied to inserted object

I have code that looks like this: public class Polynomial { List<Term> term = new LinkedList<Term>(); and it seems that whenever I do something like term.add(anotherTerm), with anotherTerm being... another Term object, it seems anotherTerm is referencing the same thing as what I've just inserted into term so that whenever I try t...

Immutable Dictionary overhead?

When using immutable dictionaries in F# , how much overhead is there when adding / removing entries? Will it treat entire buckets as immutable and clone those and only recreate the bucket whos item has changed? Even if that is the case, it seems like there is alot of copying that needs to be done in order to create the new dictionary(?...

Python references

Can someone explain why the example with integers results in different values for x and y and the example with the list results in x and y being the same object? x = 42 y = x x = x + 1 print x # 43 print y # 42 x = [ 1, 2, 3 ] y = x x[0] = 4 print x # [4, 2, 3] print y # [4, 2, 3] x is y # True ...

Why does Microsoft advise against readonly fields with mutable values?

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 fi...

Converting mutable to immutable map

private[this]object MMMap extends HashMap[A, Set[B]] with MultiMap[A, B] How convert it to immutable? ...

Building big, immutable objects without using constructors having long parameter lists

Hi StackOverflow! I have some big (more than 3 fields) Objects which can and should be immutable. Every time I run into that case i tend to create constructor abominations with long parameter lists. It doesn't feel right, is hard to use and readability suffers. It is even worse if the fields are some sort of collection type like lists....

How can I avoid mutable variables in Scala when using ZipInputStreams and ZipOutpuStreams?

I'm trying to read a zip file, check that it has some required files, and then write all valid files out to another zip file. The basic introduction to java.util.zip has a lot of Java-isms and I'd love to make my code more Scala-native. Specifically, I'd like to avoid the use of vars. Here's what I have: val fos = new FileOutputStream("...

Do fields need to be explicitly final to have a "proper" immutable object?

You often read about immutable objects requiring final fields to be immutable in Java. Is this in fact the case, or is it simply enough to have no public mutability and not actually mutate the state? For example, if you have an immutable object built by the builder pattern, you could do it by having the builder assign the individual fie...

C# delete space from variable

My variable looks like: name = "Lola "; // notice the whitespace How can I delete the whitespace at the end to leave me with just "Lola"? Thank you all, but .Trim() don't work to me. I read the text from a file, if that is any help. ...

synchronized,immutable,empty collection

Using Collections class we can make any collection synchronized,immutable or empty what are there respective uses, when we need to implement these type of collections ...

Pure functional bottom up tree algorithm

Say I wanted to write an algorithm working on an immutable tree data structure that has a list of leaves as its input. It needs to return a new tree with changes made to the old tree going upwards from those leaves. My problem is that there seems to be no way to do this purely functional without reconstructing the entire tree checking a...

Immutability after dependency injection, initialization

I'd like to be able to specify that an object's member variables are immutable once the object has been "initialized", which to me means after it has been injected with any dependencies, and has performed any other initialization operations that it can only perform after DI. Are there languages that satisfy my interest - that formalize ...

immutable strings vs std::string

I've recent been reading about immutable strings, here and here as well some stuff about why D chose immutable strings. There seem to be many advantages. trivially thread safe more secure more memory efficient in most use cases. cheap substrings (tokenizing and slicing) Not to mention most new languages have immutable strings, D2.0,...

Immutable Dot Net strings

I usually define my string variables in vb.net as Dim f_sName as string=String.Empty f_sName = "foo" Given the immutable nature of strings in .net, is there a better way to initialize strings and deal with the "Variable 'f_sName ' is used before it has been assigned a value. A null reference exception could result at runtime." warni...

Web service accepting an immutable object

Is it possible for a .NET web service to accept an immutable object (or an object containing references to immutable objects) in a web method? For example, if this is my web service method: public class MyWebService : System.Web.Services.WebService { [WebMethod] public void SetStatus(StatusData data) { // do somethi...

How can I pass a const array or a variable array to a function in C?

I have a simple function Bar that uses a set of values from a data set that is passed in in the form of an Array of data structures. The data can come from two sources: a constant initialized array of default values, or a dynamically updated cache. The calling function determines which data is used and should be passed to Bar. Bar does...

Why might a System.String object not cache its hash code?

A glance at the source code for string.GetHashCode using Reflector reveals the following (for mscorlib.dll version 4.0): public override unsafe int GetHashCode() { fixed (char* str = ((char*) this)) { char* chPtr = str; int num = 0x15051505; int num2 = num; int* numPtr = (int*) chPtr; for ...

How can I get around my "object reference" problem when passing messages to an actor?

A while back I put together a simple class named Actor that was my implementation of the Actor Model. Since then I've used it with great success (Minus some annoying workarounds for the lack of a discriminated union type.). I am left with an issue that I am unsure of how to resolve without making the class clunky and slow. When someone ...

How to instantiate a large immutable type?

I have a type with about 40 properties (all value types) that represents a type of transaction for my business. An instance of this class corresponds to a row in my database. I would like to keep my class immutable since it will only ever be used for read operations, but I am not sure how to go about setting 40 properties during initia...

Is it totally fine to use a mutable object as a key in a Dictionary?

Say I have some special class, WrappedDataTable, and I want to associate each WrappedDataTable with exactly one DataTable. Furthermore, I want there to be no more than one WrappedDataTable in existence for any given DataTable. A colleague suggested I could cache my WrappedDataTable and use a factory method to access one, like this: pub...