;Hmmmm but wait, I saw on the website that Clojure does have local
variables!
user=> (with-local-vars [a 2] (var-set a 3) (var-get a))
3

;I am allowed to close over them:
user=> (def f (with-local-vars [a 2] #(+ 1 (var-get a))))
#=(var user/f)
;But they stop me to shoot myself
user=> (f)
java.lang.IllegalStateException: Var null is unbound. (NO_SOURCE_FILE:
0)
"Executes the exprs in a context in which the symbols are bound to
vars with per-thread bindings to the init-exprs. The symbols refer to
the var objects themselves, and must be accessed with var-get and var-
set"
;The protection from leaky concurrent mutation is at execution

;Where as an intentional closure sensibly uses a ref
(let [secret (ref "nothing")]
  (defn read-secret [] @secret)
  (defn write-secret [s] (dosync (ref-set secret s))))
#=(var user/write-secret)
user=> secret
java.lang.Exception: Unable to resolve symbol: secret in this context
user=> (read-secret)
"nothing"
user=> (write-secret "hi")
"hi"
user=> (read-secret)
"hi"

So it seems Clojure doesn't force you to avoid using imperative style
variable, it is just not widely advertised that they exist :)



--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to