immutable

Best way to define an immutable class in Objective C

Hi, I am a newbie in Objective C and I was wondering what is the best way to define an immutable class in Objective-C (like NSString for example). I want to know what are the basic rules one has to follow to make a class immutable. I think that : setters shouldn't be provided if properties are used, they should be readonly to "disa...

question about book example - Java Concurrency in Practice, Listing 4.12

Hi, I am working through an example in Java Concurrency in Practice and am not understanding why a concurrent-safe container is necessary in the following code. I'm not seeing how the container "locations" 's state could be modified after construction; so since it is published through an 'unmodifiableMap' wrapper, it appears to me ...

How do I manipulate a tree of immutable objects?

I'm building an entire application out of immutable objects so that multi-threading and undo become easier to implement. I'm using the Google Collections Library which provides immutable versions of Map, List, and Set. My application model looks like a tree: Scene is a top-level object that contains a reference to a root Node. Each No...

C# - Making fields/properties read only conditionally

I have three classes; Classes A and B both reference class C. How can I make it so members of class C can be modified when referenced from class A but not modified when referenced from class B? IE, the following should be possible; classA myClassA = new classA(); myClassA.myClassC.IssueNumber = 3; But this should not be possible; c...

Why would one want to use the public constructors on Boolean and similar immutable classes?

(For the purposes of this question, let us assume that one is intentionally not using auto(un)boxing, either because one is writing pre-Java 1.5 code, or because one feels that autounboxing makes it too easy to create NullPointerExceptions.) Take Boolean, for example. The documentation for the Boolean(boolean) constructor says: Note...

Immutability and shared references - how to reconcile?

Consider this simplified application domain: Criminal Investigative database Person is anyone involved in an investigation Report is a bit of info that is part of an investigation A Report references a primary Person (the subject of an investigation) A Report has accomplices who are secondarily related (and could certainly be primary i...

Why did Matz choose to make Strings mutable by default in Ruby?

It's the reverse of this question: http://stackoverflow.com/questions/93091/why-cant-strings-be-mutable-in-java-and-net Was this choice made in Ruby only because operations (appends and such) are efficient on mutable strings, or was there some other reason? (If it's only efficiency, that would seem peculiar, since the design of Ruby se...

Why do Scala immutable HashMap methods return a Map?

Hi all! I am having problems using the update method of scala.collection.immutable.HashMap.I don't see the reason it returns a Map instead of a HashMap. How do I get a new HashMap with a new key-value pair added? ...

C#: Immutable view of a list's objects?

I have a list, and I want to provide read-only access to a collection containing its contents. How can I do this? Something like: public ICollection<Foo> ImmutableViewOfInventory() { IList<Foo> inventory = new List<Foo>(); inventory.add(new Foo()); return inventory.ImmutableView(); } Additionally, an immutable IEnumer...

Immutable classes in C++

Hi, In one of my projects, I have some classes that represent entities that cannot change once created, aka. immutable classes. Example : A class RSAKey that represent a RSA key which only has const methods. There is no point changing the existing instance: if you need another one, you just create one. My objects sometimes are heavy a...

Hashable, immutable

From a recent SO question (see http://stackoverflow.com/questions/2671211/create-a-dictionary-in-python-which-is-indexed-by-lists) I realized I probably had a wrong concept of the meaning of hashable and immutable objects in python. What hashable means in practice?, What the relation between hashable and immmutable is? There are mutab...

How do I rewrite a plist if its data types are immutable?

I am getting comfortable with using plists for initializing my app. I now want to save app state back to the plist used to initialize the app and I find myself stuck. At application startup I ingest the plist into an NSDictionary which is immutable. I now want to update the NSDictionary by replacing old values with new values for existin...

what would be a frozen dict ?

A frozen set is a frozenset. A frozen list could be a tuple. What would be a frozen dict ? An immutable, hashable dict. I guess it could be something like collections.namedtuple, but namedtuple is more like a frozenkeys dict (an half-frozen dict). No ? EDIT: A frozendict should be a frozen DICT : it should have keys, values, get, ......

How do I modify a record in erlang?

Hi, I need replace the same value for variables {place} and {other_place} in the record op. #op{ action = [walk, from, {place}, to, {other_place}], preconds = [[at, {place}, me], [on, floor, me], [other_place, {place}, {other_place}]], add_list = [[at, {other_place}, me]], del_list = [[at, {place}, me]...

Typed persistent data structure a la Clojure, done in Java?

In particular an immutable List with a cons operation would be welcome. ...

The best way to assign an immutable instance to a Collection in Java

Today I was reading through some Hibernate code and I encounter something interesting. There is a class called CollectionHelper that defines the following constant varibale: public final class CollectionHelper { public static final List EMPTY_LIST = Collections.unmodifiableList( new ArrayList(0 ) ; public static final Collection EMP...

Make All Types Constant by Default in C++

What is the simplest and least obtrusive way to indicate to the compiler, whether by means of compiler options, #defines, typedefs, or templates, that every time I say T, I really mean T const? I would prefer not to make use of an external preprocessor. Since I don't use the mutable keyword, that would be acceptable to repurpose to indic...

How would I yield an immutable.Map in Scala?

I have tried this but it does not work: val map:Map[String,String] = for { tuple2 <- someList } yield tuple2._1 -> tuple2._2 How else would I convert a List of Tuple2s into a Map? ...

Persistent (purely functional) Red-Black trees on disk performance

I'm studying the best data structures to implement a simple open-source object temporal database, and currently I'm very fond of using Persistent Red-Black trees to do it. My main reasons for using persistent data structures is first of all to minimize the use of locks, so the database can be as parallel as possible. Also it will be eas...

Java: immutable stack?

TERM Immutable Stack: I overuse stack. Poly's reply uses Collections.emptyList() as immutable list. No Collections.emptyStack(). Combining the words stack and immutability, from the last experiences, gets "immutable stack" (probably not related to functional prog). Java Api 5 for list interface shows that Stack is an implementing class...