immutable

NSString immutable allows to change its values?

The code below compiles and runs, BUT according to all iPhone development books and Apple documentation it shouldnt! Can someone please explain to me how come immutable NSString allows to change its values after it has been set? I thought I had to use NSMuttableString to change context of the same string variable? I am using SDK 3.1. N...

Undo/Redo with immutable objects

I read the following in an article Immutable objects are particularly handy for implementing certain common idioms such as undo/redo and abortable transactions. Take undo for example. A common technique for implementing undo is to keep a stack of objects that somehow know how to run each command in reverse (the so-call...

How would you convert this mutable tree into an immutable one?

How would you convert type Node into an immutable tree? This class implements a range tree that does not allow overlapping or adjacent ranges and instead joins them. For example if the root node is {min = 10; max = 20} then it's right child and all its grandchildren must have a min and max value greater than 21. The max value of a rang...

Should IEquatable<T>, IComparable<T> be implemented on non-sealed classes?

Anyone have any opinions on whether or not IEquatable or IComparable should generally require that T is sealed (if it's a class)? This question occurred to me since I'm writing a set of base classes intended to aid in the implementation of immutable classes. Part of the functionality which the base class is intended to provide is autom...

Can i make string behave like Reference Type?

In C#, strings are reference type but behaves like value type. e.g. string str = "I am a string"; str.Replace("am", "was"); //str still contains same value, //i want it to make "I was a string" I know i can do this by str = str.Replace("am", "was"); But i don't want to re-assign it. Is there anyway to make them behave like Refere...

Scala bug with immutable Map in concurrent program?

I've written a Monte Carlo player for the board game Nine Men's Morris. Everything is basically immutable. The program involves lots of futures (hundreds) and a lot of modifying immutable Maps. Sometimes I get a crash with the following exception: java.lang.NullPointerException at scala.collection.mutable.HashTable$class.elemHashCod...

Any "Gotchas" with using Freezable class outside of WPF?

I've been reading up on concurrency, and looking at things from a more "thread safe" point of view. WPF (or actually System.Windows.Freezable and others) has a freezable class, which can give "popsicle immutablity". Has anyone tried using this outside of WPF/Silverlight and would it be better to use this, or roll your own/use someone e...

How do I delete the last character of a particular String in Java?

For example I'm extracting a text String from a text file and I need those words to form an array. However, when I do all that some words end with comma (,) or a full stop (.) or even have brackets attached to them (which is all perfectly normal). What I want to do is to get rid of those characters. I've been trying to do that using tho...

How to create an immutable map/set from a seq?

I am try to construct immutable Sets/Maps from a Seq. I am currently doing the following: val input: Seq[(String, Object)] = //..... Map[String, Object]() ++ input and for sets val input: Seq[String] = //..... Set[String]() ++ input Which seems a little convoluted, is there a better way? ...

An efficient technique to replace an occurence in a sequence with mutable or immutable state

I am searching for an efficient a technique to find a sequence of Op occurences in a Seq[Op]. Once an occurence is found, I want to replace the occurence with a defined replacement and run the same search again until the list stops changing. Scenario: I have three types of Op case classes. Pop() extends Op, Push() extends Op and Nop()...

Is static initialized unmodifiableCollection.get guaranteed immutable?

Is static initialized unmodifiableCollection.get guaranteed immutable? For: static final Map FOO = Collections.unmodifiableMap(new HashMap()); Can multiple threads use method get and not run into problems? Even through items in FOO cannot be added/removed, what's stopping the get method from manipulating FOO's internal state for ca...

Cocoa: Testing to find if an NSString is immutable or mutable?

This produces an immutable string object: NSString* myStringA = @"A"; //CORRECTED FROM: NSMutableString* myStringA = @"A"; This produces a mutable string object: NSMutableString* myStringB = [NSMutableString stringWithString:@"B"]; But both objects are reported as the same kind of object, "NSCFString": NSLog(@"myStringA is type: ...

What are immutable objects?

What is the relationship with thread-safety and immutable objects? Does it makes easier to share a single resource among multiple threads? If immutable objects are stateless, can they be pooled in a container like a J2EE container? thanks ...

Compiler-Implemented Immutability in .Net

Given this class... public class Test { private long _id; public Test(long id) { _id = id; } } Will the .Net compiler actually compile it as... public class Test { private readonly long _id; public Test(long id) { _id = id; } } In other words, does it understand that _id is only ever set from the construct...

Overcoming the "disadvantages" of string immutability

I want to change the value of a particular string index, but unfortunately string[4] = "a" raises a TypeError, because strings are immutable ("item assignment is not supported"). So instead I use the rather clumsy string = string[:4] + "a" + string[4:] Is there a better way of doing this? ...

What are the advantages of built-in immutability of F# over C#?

I heard F# has native support for immutability but what about it that can not be replicated in C#? What do you get by an F# immutable data that you don't get from a C# immutable data? Also in F#, is there no way to create mutable data? Is everything immutable? If you use both C# and F# in an application, can you change the immutable F# ...

How does F# deal with the thousands of standard mutable types in the BCL?

Like Point, Size, etc value types. I also heard strings in .NET aren't truly immutable. Does F# use these or alternative immutable versions of them? If it uses the standard mutable BCL types, would this not compromise the whole immutability trust that F# gives both at compile and runtime? EDIT: What I meant to ask was, if you have alt...

Automapper and immutability

Is it possible to use automapper with Immutabile types. For example my Domain type is immutable and I want to map my view type to this. I believe it is not but just want this confirmed. Also as it is best pratice to have your domain types immutabile, what is the best pratice when mapping your view types to domain types? ...

Immutable Class Construction Design

So we all realize the benefits of immutable types, particularly in multithreaded scenarios. (Or at least we should all realize that; see e.g. System.String.) However, what I haven't seen is much discussion for creating immutable instances, specifically design guidelines. For example, suppose we want to have the following immutable cla...

How to make Scala's immutable collections hold immutable objects.

I'm evaluating Scala and am having a problem with its immutable collections. I want to make immutable collections, which are completely immutable, right down through all the contained objects, the objects they reference, ad infinitum. Is there a simple way to do this? The code on http://www.finalcog.com/immutable-containers-scala il...