3 ms·
No real argument here, but Haskell does encourage you to use read only data structures unless you truly need writes. That said, there's still a wide variety of
by awj 13y ago
No real argument here, but Haskell does encourage you to use read only data structures unless you truly need writes. That said, there's still a wide variety of clever ways in which you can find your way to multithreading hell.
- jtheory 13y agoRead-only data structures are a common idiom in Java as well (the classic example is String), and do indeed help reduce the headaches.
- windust 13y agoNot sure this is true. There is the concept of final fields, but that doesn't make objects immutable, and thus even when declaring object finals (or arrays) you can pretty easily shoot yourself in the foot by mutating them in-place.
- mbucc 13y agoFor immutable collections, guava is helpful.
- jtheory 13y agoIt is -- but you still do have to be aware if the objects in your collection are also immutable... if so, you may end up with an "immutable" list of 5 objects, and down the line it will still have those 5 objects, but the objects themselves may have completely different internal data. Referring partly to your parent comment... it's instructive to look at the String implementation. It's immutable, but they have to jump through a few hoops to do it properly... internally, a String has a private char array. An array is not immutable, so the code has to be very careful to never expose that char array externally. Because of that, calling toCharArray() will copy the internal array and return the copy -- that one's fairly obvious. But less obviously, a String constructed with a char array must also make a copy... otherwise the calling code might then modify the array after creating the String with it. So sure, immutable objects are possible, and widely encouraged in Java, but they're easy to do wrongly/incompletely.
- ddeck 13y agoAs Josh Bloch said: Classes should be immutable unless there's a very good reason to make them mutable Immutability makes life a LOT easier. A great example is the well designed joda-time vs the disaster that is the the mutable Date and Calendar core language classes.
- ScottBurson 13y agoYes. Now if only the standard Java collections were immutable...