Re:Coding Skill and the Decline of Stagnation
by pigs on Friday January 13, 2012 @06:30PM
(#3462874)
Oops, meant to say "adding a setter violates immutability"
Re:Coding Skill and the Decline of Stagnation
by masklinn on Saturday January 14, 2012 @04:46AM
(#3463915)
Bean-type setters, definitely.
On the other hand, you could have "setters" using the same naming convention which do a clone-and-replace (and return the new object), that would not violate immutability (and would be easier than building objects from scratch every time from the outside) e.g.
Type setFoo(FooType foo) {
return new Type(
this.field0,
this.field1,
this.field2,
foo, // bam
this.field4);
}
many functional languages have that behavior when manipulating "records".
Uh what? No it does not. Writing stupid getters (or classes) might, but writing getters does not "violate immutability".
Naturally, getters are not of much use if all your fields are final and hold immutable objects, but the latter can be tricky in many OO languages.
Getters significantly improve the situation there, by providing a point at which you can clone your internal state and return a copy, letting you keep your object immutable even if you have mutable fields (of course this assumes you can deeply copy all your mutable member objects)