On Sun, Sep 12, 2010 at 3:58 PM, doc <mdpendergr...@gmail.com> wrote:
> Hello,
>
> I am relatively new to clojure and lispy languages in general.  I
> wrote a small program that manipulated thread vars using the binding
> macro and I am seeing what seems to be strange behavior.
>
> To illustrate, here is another small program that reproduces what I am
> seeing (without all the distraction of the original).  There is a
> reason for the nested bindings in the original code where it seems a
> bit contrived here.  Regardless, if it is something I am doing wrong,
> please let me know.
>
> ------------
>
> (declare x y)
>
> (def rnd1 (java.util.Random.))
> (def rnd2 (java.util.Random.))
>
> (defn throw-random-exception [c]
>  (let [value (.nextInt rnd1 100)]
>    (if (= value 50)
>      (throw (Exception. (str "Random Exception after count = "
> c))))))
>
> (defn change-value-of-thread-local-var []
>  (set! x (conj x (.nextInt rnd2 100))))
>
> (defn test-binding []
>  (binding  [x [0 1 2 3 4 5 6]]
>    (try
>      (loop [c 0]
>        (do
>          (change-value-of-thread-local-var)
>          (throw-random-exception c))
>        (recur (inc c)))
>      (catch Exception e (println (str x "\n" (.getMessage e)))))))
>
> (defn test-nested-binding []
>  (binding  [x [0 1 2 3 4 5 6]]
>    (try
>      (loop [c 0]
>        (binding [y [7 8 9]]
>          (do
>            (change-value-of-thread-local-var)
>            (throw-random-exception c))
>          (recur (inc c))))
>      (catch Exception e (println (str x "\n" (.getMessage e)))))))
>
>
> ------------
>
> If I load this as a file at the REPL and then run "(test-binding)" the
> value of x is unbound when the function returns.  However if you run
> "(test-nested-binding)" then the value of BOTH x and y have a bound
> value when the function returns.  Is this a bug?  If so, where do I
> report it?  If not, what am I doing wrong?  I have also moved the
> binding of x inside the "try" and it has the same behavior.

For me, x and y are unbound after running either function on a recent
master branch of Clojure.  What version of Clojure are you using?

user=> (clojure-version)
"1.3.0-master-SNAPSHOT"

If you're using something earlier than the 1.2 release, I'd suggest
you upgrade to 1.2 and try again.

--Chouser
http://joyofclojure.com/

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

Reply via email to