Is everything in Clojure immutable? For example, w/ this code-snippet
(let [x nil]
;; do something and modify 'x'
)
how does one modify the value of 'x' ?
(let [x nil] (def x true))
this doesn't work. the "def' interns and defines a (dynamic) root-
binding
for 'x' which is accessible outside the lexical closure.
I read in this page (http://clojure.org/special_forms) that lexical
bindings
are write-once/read-many.
(in bold print)
"Locals created with let are not variables. Once created their
values never change!"
This description of 'set!' (http://clojure.org/vars#set) reinforces
that statement.
Sure enough - it's true.
user=> (let [x nil] (set! x true))
java.lang.IllegalArgumentException: Invalid assignment target
(NO_SOURCE_FILE:7)
This leads me to observe that one cannot simply declare a name/symbol
as lexical
as one does in other lisps, a la
(let (x y) (setq x 1 y 2))
One could use the idiom that I used above, i.e., to assign "nil". But
that's useless
b/c it's read-only beyond that expression.
So I want to ask, am I in the minority in thinking that read-only
local bindings limit
the programmer a lot. The only alternative that I see (w/ my limited
imagination)
is to create a "let" scope at the exact place that one needs it. But,
IMO, that
sucks - sorry for the vernacular; but it is wildly inconvenient for me
for many reasons.
Do both of these statements intern a symbol? Or does the 2nd set the
currently
interned symbol to a new value ?
(def x 1)
(def x 2)
Is the proper way to change the value bound to a symbol to use
"set!" ?
(set! x 3)
If I understand correctly, the "(binding ...)" macro specifically
applies to
"Variables", one of Clojure's STM types, thus has dynamic, thead-local
scope. Is that right? In which case, it may hide a lexical binding ?
Thanks for any help. These details stop me from reaching that
critical
point in which code flows from my mind into the computer.
CHICKEE
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---