3

I'm working through the Clojure examples in Stuart Halloway's "Programmming Clojure" and I've hit a snag when using binding:

(def foo 10)
; => '#user/foo

foo
; => 10

(binding [foo 42] foo)
; => 10

(binding [user/foo 42] foo)
; => 10

(binding [user/foo 42] (var-get #'user/foo))
; => 10

Why won't it give me 42?

4
  • In the second line, two characters seem to be switched. Commented Oct 21, 2009 at 6:36
  • 1
    What IDE / environment are you using? What version of Clojure? Your code works fine for me, latest Clojure from git, running from commandline. Commented Oct 21, 2009 at 6:45
  • Clojure-1.1.0-alpha-SNAPSHOT on Snow Leopard... I just tried it on my Windows PC on 1.0.0 and it worked fine, so it must be a bug with that version. Funny thing is is that the book's author recommended that you use the bundled version of Clojure because he tested all his examples on it... and then it doesn't work for that example. Commented Oct 21, 2009 at 6:51
  • Same behavior on Linux/1.1.0-alpha, perhaps you should ask about this on groups.google.com/group/clojure Commented Oct 21, 2009 at 20:12

2 Answers 2

3

Verdict: bug

This appears to be a bug in the 1.1.0-alpha-SNAPSHOT and it is reproducible on Linux, too.

If you go back now to the Clojure d/l page, that version seems to have been withdrawn and 1.0.0 is the "Featured" d/l.

And in 1.0.0 your example does bind 42.

Sign up to request clarification or add additional context in comments.

Comments

1

On a related note:
its really easy to be bitten by bindings in this way when ever you run code in another thread. I have encountered problems like this when a function I call evaluates something through pmap instead of map when the code is actually executed on a thread from the thread-pool. agents will do this also i believe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.