On Mon, Dec 29, 2008 at 2:57 PM, Dave Griffith
<dave.l.griff...@gmail.com> wrote:
>
> It looks like the mutable locals use case is covered by the "with-
> local-vars" binding form.

Not really.  with-local-vars has somewhat surprising semantics.

For example, you'd expect this (contrived) function to generate an
"add 2" function:
(defn create-add-2 []
  (with-local-vars [x 1]
    (do
      (var-set x 2)
      (fn [y] (+ y (var-get x))))))

But in fact, it just generates a function which errors.

If Clojure had some sort of "mutable local" binding construct, I would
expect this to work:
(defn create-add-2 []
  (mutable [x 1]
     (do
       (set! x 2)
       (fn [y] (+ x y)))))
because you should be able to refer to a mutable local inside of a closure.

But any attempt to *set* the local in the closure would generate an
error, because you really should be using refs for something like
this:
(defn create-growing-adder []
  (mutable [x 1]
     (do
       (set! x 2)
       (fn [y] (do (set! x (inc x)) (+ x y))))))


I think if Clojure could do something like this (enforce a certain
kind of referentially transparent mutable local), that would be neat,
but just extending the interface for atoms with atom-set (as I
proposed in my previous post) is probably a perfectly fine and more
realistic solution.

--~--~---------~--~----~------------~-------~--~----~
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 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to