On Sun, Mar 24, 2013 at 11:04 AM, Jim - FooBar(); <jimpil1...@gmail.com>wrote:
> On 24/03/13 17:49, Shantanu Kumar wrote: > >> In this case, making the type immutable is probably encouraged but not >> mandatory >> > > yes true, it's not enforced or anything like that, but I'd say it is more > than just 'encouraged'... what would be the point of using clojure's > reference types with something mutable? there is nothing to be gained from > that, is there? the indirection of vars/refs only makes true sense when > dealing with values and pure functions... > It might not be obvious at first, but Clojure's "immutable" types are *not* immutable in the sense of "they cannot be modified in place from within the JVM", but only in the sense of "they cannot be modified in place by using Clojure's functions alone on them". This surprised me at first when I learned it. For example, try this at a REPL: user=> (def v [1 2 3]) #'user/v user=> (class v) clojure.lang.PersistentVector user=> v [1 2 3] user=> (aset (.tail v) 1 -2) -2 user=> v [1 -2 3] So Clojure's built-in collection types are immutable in the sense of "they are immutable if you don't do anything that mutates them". In the very same sense, *any* Java Object can potentially be immutable -- just don't do anything that mutates it. I agree that there are some types that are much easier to avoid mutating than others, because it is made inconvenient to do so. However, if you are willing to, and have JVM permissions to, use reflection in certain ways, you can even modify private fields of an object. Andy -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.