immutable

Immutable functional objects in highly mutable domain

Hi, I'm currently learning functional programming in my spare time with Scala, and I have an idle newbie question. I can see the elegance of having immutable objects when doing something like calculating a Haar wavelet transform - i.e. when the data itself being represented by the objects doesn't change. But I saw a blog where someone ...

How do I identify immutable objects in Java

In my code, I am creating a collection of objects which will be accessed by various threads in a fashion that is only safe if the objects are immutable. When an attempt is made to insert a new object into my collection, I want to test to see if it is immutable (if not, I'll throw an exception). One thing I can do is to check a few well-...

Are immutable arrays possible in .NET?

Is it possible to somehow mark a System.Array as immutable. When put behind a public-get/private-set they can't be added to, since it requires re-allocation and re-assignment, but a consumer can still set any subscript they wish: public class Immy { public string[] { get; private set; } } I thought the readonly keyword might do t...

Mutable vs immutable objects

I'm trying to get my head around mutable vs immutable objects. Using mutable objects gets a lot of bad press (e.g. returning an array of strings from a method) but I'm having trouble understanding what the negative impacts are of this. What are the best practices around using mutable objects? Should you avoid them whenever possible? ...

Immutable object pattern in C# - what do you think?

I have over the course of a few projects developed a pattern for creating immutable (readonly) objects and immutable object graphs. Immutable objects carry the benefit of being 100% thread safe and can therefore be reused across threads. In my work I very often use this pattern in Web applications for configuration settings and other obj...

Efficient persistent data structures for relational database

I'm looking for material on persistent data structures that can be used to implement a relational model. Persistence in the meaning of immutable data structures. Anyone know of some good resources, books, papers and such? (I already have the book Purely Functional Data Structures, which is a good example of what I'm looking for.) ...

Immutable beans in Java

I am very curious about the possibility of providing immutability for java beans (by beans here I mean classes with an empty constructor providing getters and setters for members). Clearly these classes are not immutable and where they are used to transport values from the data layer this seems like a real problem. One approach to th...

How do I create an immutable Class ?

Hi, I am working on creating an immutable class. I have marked all the properties as read-only. I have a list of items in the class. Although if the property is read-only the list can be modified. Exposing the IEnumerable of the list makes it immutable. I wanted to know what is the basic rules one has to follow to make a class imm...

Functional programming: state vs. reassignment

I need help getting my head around the difference between my current OOP notion of state, and the way it would be done in a functional language like Haskell or Clojure. To use a hackneyed example, let's say we're dealing with simplified bank account objects/structs/whatever. In an OOP language, I'd have some class holding a reference ...

How to design an immutable object with complex initialization

I'm learning about DDD, and have come across the statement that "value-objects" should be immutable. I understand that this means that the objects state should not change after it has been created. This is kind of a new way of thinking for me, but it makes sense in many cases. Ok, so I start creating immutable value-objects. I make s...

Functional programming: immutability etc.

I recently asked a question about functional programming, and received (good!) answers that prompted more questions (as seems to be the case with learning, sometimes). Here are a couple examples: One answer made reference to an advantage of immutable data structures: each thread can have its own copy. Now, to me, this sounds rather lik...

String Immutability

Does string immutability work by statement, or by strings within a statement? For example, I understand that the following code will allocate two strings on the heap. string s = "hello "; s += "world!"; "hello" will remain on the heap until garbage collected; and s now references "hello world!" on the heap. However, how many strings...

How to implement a readonly (immutable) object interface in C#

What I need is to make sure that in most scenarios objects are used via "readonly interface", which is a subset of the full interface. If I were in C++, I would just return a const object, for instance. If I could use interfaces, I would just implement a readonly interface and use it everywhere, however, I need operator overloading, wh...

How to get a c++ constant pointer equivalent in Java?

When I pass an immutable type object(String, Integer,.. ) as final to a method I can achieve the characters of a C++ constant pointer. But how can I enforce such behavior in objects which are mutable? public void someMethod(someType someObject){ /* * code that modifies the someObject's state * */ } All I want is to prevent som...

Ruby - Immutable Objects

I've got a highly multithreaded app written in Ruby that shares a few instance variables. Writes to these variables are rare (1%) while reads are very common (99%). What is the best way (either in your opinion or in the idiomatic Ruby fashion) to ensure that these threads always see the most up-to-date values involved? Here's some ideas ...

Why are mutable structs evil?

Following the discussions here on SO I already read several times the remark that mutable structs are evil (like in the answer to this question). What's the actual problem with mutability and structs? ...

How to test whether a Ruby object is immutable?

Is there an easy way to test whether an object is a immutable (numbers, nil) or not (Array, Hash, objects)? In other words, could it be changed by side effects from other code? Motivation: I want to create a versioned value store, but some of the data is arrays. Some of the arrays will store custom objects, and I could invert the rela...

How can I write an app that doesn't change state (in functional language)?

I want to learn functional programming one day but I don't understand how I could use it for anything other than simple math. For example: A simple web browser add bookmark function needs to cause some sort of mutation so that next time the user clicks bookmarks the new bookmark is there in the list. ...

How do I find out if a class is immutable in C#?

How do I find out if a class is immutable in C#? ...

What's the use of System.String.Copy in .NET?

I'm afraid that this is a very silly question, but I must be missing something. Why might one want to use String.Copy(string)? The documentation says the method Creates a new instance of String with the same value as a specified String. Since strings are immutable in .NET, I'm not sure what's the benefit of using this method, a...