So you're saying that immutable objects are impossible to track if they contain mutable collections...
Isn't that a bit like saying waterproof jackets are hard to keep dry if you stuff them full of water balloons? On 12 October 2010 17:25, Reinier Zwitserloot <[email protected]> wrote: > Perhaps now is a good time to show off this little snippet just to > highlight how nigh-impossible it is to track immutability: > > public class SeeminglyImmutablePointer { > private static final Map<SeeminglyImmutablePointer, Object> map = > new MapMaker().weakKeys().makeMap(); > > public void set(Object val) { > map.put(this, val); > } > public Object get() { > return map.get(this); > } > } > > Note that @SEF and @Unchanging have no problem with this snippet; a > compiler can clearly determine that set is not SEF, and that get() is, > and it can also determine that set() is unchanging (trivially - any > method with return type void will have that property), and that get > isn't. > > A real problem here is that java lacks immutable collection types. > i.e. map.get(this) *WOULD* be unchanging if youre querying an > immutable map (Collections.unmodifiable isn't good enough, that's a > read-only view of a possibly mutating map), but there's no way to > reflect this in the type system, and thus the compiler could never > tell you, you'd have to make an assertion. > > Guava / com.google.collect does have such types, though the complete > API picture if you add these is a bit ugly - preferably List does not > imply immutability, but nevertheless does not have set, clear, and > insert, then a new interface named MutableList subtypes it that adds > these and implies mutability, and a new interface named ImmutableList > subtypes List without adding new methods, but implying immutability. -- Kevin Wright mail / gtalk / msn : [email protected] pulse / skype: kev.lee.wright twitter: @thecoda -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
