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 ...
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-...
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...
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?
...
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...
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.)
...
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...
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...
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 ...
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...
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...
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...
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...
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...
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 ...
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?
...
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...
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#?
...
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...