By "immutable function" or "immutable method", I mean a function whose result will never vary if you give it the same arguments.
I would be interested to know if anyone know of a more generic or less verbose solution when you want to cache the precomputed value(s) of an immutable function.
Let me explain what I mean with a simple examp...
I'm trying to subclass str, but having some difficulties due to its immutability.
class DerivedClass(str):
def __new__(cls, string):
ob = super(DerivedClass, cls).__new__(cls, string)
return ob
def upper(self):
#overridden, new functionality. Return ob of type DerivedClass. Great.
caps = super(D...
I've recently been learning about functional programming (specifically Haskell, but I've gone through tutorials on Lisp and Erlang as well). While I found the concepts very enlightening, I still don't see the practical side of the "no side effects" concept. What are the practical advantages of it? I'm trying to think in the functional m...
I have a class in C# that contains a Dictionary, which I want to create and ensure nothing as added, edited or removed from this dictionary as long as the class which contains it exists.
readonly doesn't really help, once I tested and saw that I can add items after. Just for instance, I created an example:
public class DictContainer
{
...
We have lots of immutable value objects in our domain model, one example of this is a position, defined by a latitude, longitude & height.
/// <remarks>When I grow up I want to be an F# record.</remarks>
public class Position
{
public double Latitude
{
get;
private set;
}
// snip
public Position(dou...
I frequently read that structs should be immutable - aren't they by definition?
Do you consider int to be immutable?
int i = 0;
i = i + 123;
Seems okay - we get a new int and assign it back to i. What about this?
i++;
Okay, we can think of it as a shortcut.
i = i + 1;
What about the struct Point?
Point p = new Point(1, 2);
p.O...
In the application I am writing, I need to write lots of base types, which will most likely be immutable. But I am wondering how mutable types compare in parallel applications to immutable ones.
You can use locks with mutable objects, right? How does it compare to other techniques used with immutable types in parallel applications?
You...
Gday All,
I have been dabbling in some F# of late and I came up with the following string builder that I ported from some C# code. It converts an object into a string provided it passes a Regex defined in the attributes. Its probably overkill for the task at hand but its for learning purposes.
Currently the BuildString member uses a mu...
I've read in several books... and online... about immutable and mutable strings.
They claim "immutable strings" can't be changed.
(But they never define "change".)
Which of these NSStrings could be changed without using NSMutableString?
The string contains "catfish"... and I later try to change it to "cat".
(Same letters, just shorter....
If you have an immutable type like this:
struct Point3
{
}
and a member inside like origin:
public static const Point3 Origin = new Point3 (0,0,0);
should you use:
new Point3 (0,0,0)
?
It seems to me that since the type can not be changed, why have many origins that are essentially the same thing? Like we never change 0, right...
I am wondering how immutability is defined? If the values aren't exposed as public, so can't be modified, then it's enough?
Can the values be modified inside the type, not by the customer of the type?
Or can one only set them inside a constructor? If so, in the cases of double initialization (using the this keyword on structs, etc) is ...
For some reason, I've always assumed that readonly fields have overhead associated with them, which I thought of as the CLR keeping track of whether or not a readonly field has been initialized or not. The overhead here would be some extra memory usage to keep track of the state and a check when assigning a value.
Perhaps I assumed thi...
I am making most of my basic types in my app, immutable. But should the collections be immutable too? To me, this seems like a huge overhead unless I am missing something.
I am talking about collections to hold Point3 values, etc which can be added as it goes at different times. So if there are 1M values in a collection, and you needed ...
Are there any considerations for immutable types regarding hash codes?
Should I generate it once, in the constructor?
How would you make it clear that the hash code is fixed? Should I? If so, is it better to use a property called HashCode, instead of GetHashCode method? Would there be any drawback to it? (Considering both would work, b...
Does it make sense to implement a copy method on an immutable type, returning a new instance? Or should it just be the current instance?
I thought the type doesn't change anyway so why copy? Like no one copies the number 5, right?
...
I've implemented a class in Java which, internally, stores a List. I want the class to be immutable. However, I need to perform operations on the internal data which don't make sense in the context of the class. Hence, I have another class which defines a set of algorithms. Here is a simplified example:
Wrapper.java
import java.uti...
Should you be allowed to delete an item from the collection you are currently iterating in a foreach loop?
If so, what should be the correct behavior?
...
Is there a way to efficiently share or move .NET objects across AppDomains? I realize that the intent of AppDomains is to provide isolation - however I have a case where I need to move a relatively large, cached set of immutable objects that are expensive to compute and create. At the moment, I have a serialization approach that works, b...
I don't know if I worded it correctly, but for a simple example let's say we have a collection of Point3 values (say 1M).
We have a method called Offset that adds another Point3 value on these values, returning new Point3 values. Let's say the method is static.
The Point3 type is immutable.
The question is, should I have a method like...
Allen Holub wrote the following,
You can't have a program without some coupling. Nonetheless, you can minimize coupling considerably by slavishly following OO (object-oriented) precepts (the most important is that the implementation of an object should be completely hidden from the objects that use it). For example, an object's insta...