Hi,
I am wondering about the benefits of having the string-type immutable from the programmers point-of-view.
Technical benefits (on the compiler/language side) can be summarized mostly that it is easier to do optimisations if the type is immutable. Read here for a related question.
Also, in a mutable string type, either you have thre...
I was just looking at Eric Lippert's simple implementation of an immutable binary tree, and I have a question about it. After showing the implementation, Eric states that
Note that another nice feature of
immutable data structures is that it
is impossible to accidentally (or
deliberately!) create a tree which
contains a cycl...
I was curious how the StringBuilder class is implemented internally, so I decided to check out Mono's source code and compare it with Reflector's disassembled code of the Microsoft's implementation. Essentially, Microsoft's implementation uses char[] to store a string representation internally, and a bunch of unsafe methods to manipulate...
class Unit {
private readonly string name;
private readonly double scale;
public Unit(string name, double scale) {
this.name = name;
this.scale = scale,
}
public string Name { get { return name; } }
public string Scale { get { return scale; } }
private static Unit gram = new Unit("Gram", 1.0...
At the moment, Im trying to understand Functional Programming in Scala and I came across a problem I cannot figure out myself.
Imagine the following situation:
You have two classes: Controller and Bot. A Bot is an independent Actor which is initiated by a Controller, does some expensive operation and returns the result to the Controll...
I've recently started using F# for "real work" and rediscovered the beauty of immutable data structures such as the discriminated unions and records in F#. I've also found them to be quite straight forward to use from C#, especially as they don't require any direct dependencies on the F# runtime. However, when it comes to representing li...
I'm writing a game that has a huge 2D array of "cells". A cell takes only 3 bytes. I also have a class called CellMap, which contains the 2D array as a private field, and provides access to it via a public indexer.
Profiling showed that a performance problem is caused by garbage collection of too many Cell objects. So I decided to make ...
consider this piece of code:Welcome to Scala version 2.8.0.r0-b20100714201327 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_20).
scala> val a = IntMap((1,1))
a: scala.collection.immutable.IntMap[Int] = IntMap((1,1))
scala> a.map(x => (x._1,x._2 + 1))
res23: scala.collection.immutable.Map[Int,Int] = Map((1,2))
header of IntMap.map says t...
So I've been reading Effective Java by Joshua Bloch and noticed two points which I actually have encountered in my work.
Point 1: Making setter methods to make code more readable.
In his example, we have a class with a ridiculously huge constructor. When people instantiate the class, it's hard to tell what's going on with all the para...
Hello, Clojure truly piqued my interest, and I started going through a tutorial on it:
http://java.ociweb.com/mark/clojure/article.html
Consider these two lines mentioned under "Set":
(def stooges (hash-set "Moe" "Larry" "Curly")) ; not sorted
(def more-stooges (conj stooges "Shemp")) ; -> #{"Moe" "Larry" "Curly" "Shemp"}
My first th...
Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn't actually prevent one from doing something like
final int[] array = new int[] {0, 1, 2, 3};
array[0] = 42;
I want the elements of the array to be unchangeable.
...
The Builder implements Cloneable and overrides clone() and instead of copying every field of the builder, the immutable class keeps a private clone of the builder. This makes it easy to return a new builder and create slightly modified copies of an immutable instance.
This way I can go
MyImmutable i1 = new MyImmutable.Builder().foo(1)....
I was just curious to know why structs, strings etc are immutable? What is the reason for making them immutable and rest of the objects as mutable. What are the things that are considered to make an object immutable?
Is there any difference on the way how memory is allocated and deallocated for mutable and immutable objects?
...
I am unable to get what are the scenarios where we need an immutable class.
Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern.
...
Is it true that if I only use immutable data type, my Java program would be thread safe?
Any other factors will affect the thread safety?
*Would appreciate if can provide an example. Thanks!
*
...
Imagine a simple (made up) language where functions look like:
function f(a, b) = c + 42
where c = a * b
(Say it's a subset of Lisp that includes 'defun' and 'let'.)
Also imagine that it includes immutable objects that look like:
struct s(a, b, c = a * b)
Again analogizing to Lisp (this time a superset), say a struct definitio...
Update: It occurred to me after posting this question that the main downside of this idea would simply be that such a type would be easy to use improperly. That is, the type would have to be used in a very specific way to draw any benefits. What I originally had in mind was something that would be used like this (sticking with the Square...
I have no idea what immutable class should look like but am pretty sure this one is. Am I right? If I'm not please specify what should be added/removed.
import java.io.Serializable;
public class Triangle implements IShape, Serializable {
private static final long serialVersionUID = 0x100;
private Point[] points;
public Tr...
I want to have immutable types that can, ideally, sort out their own hashing and equality, but can be easily subclassed. I started off using namedtuple:
class Command(namedtuple('Command', 'cmd_string')):
def valid_msg(msg):
return True
def make_command(msg):
if self.valid_msg(msg):
return '%s:%s' % (self.cmd_string,...
Nostalgic for Collections.unmodifiableMap(), I've been implementing a read-only IDictionary wrapper based on this discussion, and my unit test quickly ran into a problem:
Assert.AreEqual (backingDictionary, readOnlyDictionary);
fails, even though the key-value pairs match. I played around a little more, and it looks like at least (tha...