Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure binding not working

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?

like image 570
cdmckay Avatar asked Nov 28 '25 20:11

cdmckay


2 Answers

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.

like image 103
DigitalRoss Avatar answered Nov 30 '25 23:11

DigitalRoss


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.

like image 41
Arthur Ulfeldt Avatar answered Nov 30 '25 21:11

Arthur Ulfeldt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!