On 24/03/13 22:59, Andy Fingerhut wrote:
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]
OMG! I read your message very quickly late last night and honestly I
thought you were pulling my leg...I just tried this at a repl though,
and you're indeed right...Am I the only one who finds this slightly
disturbing? For almost 4 years, I've been hearing of how beautifully
immutable (and performant) clojure's data-structures are. Being able to
modify a vector in-place contradicts all that doesn't it? How can
something be immutable when it exposes a public field (.tail) that can
be mutated in place? Why is tail public anyway?
I feel like the ground has disappeared right under my feet...All the
beautiful characteristics/properties of clojrue's collections seem to go
hand in hand with proper usage rather than enforced immutability. Even
though that is a 'win', it's not a big one since one can argue that an
arraylist which is not mutated in place can serves the purpose equally
good...
Moreover, consider the following scenario:
-someone has heard about clojure's collections and wants to use them
from Java.
-he opens up his favourite IDE, imports Clojure and starts using a
persistent vector-let's call it 'v'.
-he types 'v.' and the IDE autocomplete feature immediately comes up
with suggestions.
-he immediately notices that .tail is accessible and assumes that this
is the proper way of 'updating' the vector.
-now, the persistence has disappeared completely hasn't it?
what is the point in that? Why do we call it 'persistent' when the
persistence is only guaranteed through proper usage? Isn't an arraylist
persistent if I use it as such?
I am seriously puzzled...I gave a presentation about Clojure last year
at Manchester University and the thing I spent the most time on was the
foundation of Clojure - its data-structures. I basically concluded that
this is the 1 thing in Clojure that essentially makes everything else
play nicely... I feel slightly 'betrayed' now...ok 'betrayed' may be a
bit of a stretch but you get the idea...I can no longer claim that
Clojure's collections are values, can I?
Jim
--
--
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.