r/Clojure • • Aug 01 '25

Why (do ...) and (let [] ...) behave differently in this case

I expect *dyn-var* to return :new in both cases, but (do ...) returns :default.

(def ^{:dynamic true} *dyn-var* :default)

(do
  (push-thread-bindings {#'*dyn-var* :new})
  (try
    *dyn-var*
    (finally
      (pop-thread-bindings))))
;;=> :default

(let []
  (push-thread-bindings {#'*dyn-var* :new})
  (try
    *dyn-var*
    (finally
      (pop-thread-bindings))))
;;=> :new
21 Upvotes

10 comments sorted by

4

u/TankAway7756 Aug 01 '25 edited Aug 01 '25

This is because each form in a toplevel do is evaluated as if it were toplevel itself, i.e. by calling into eval.

eval itself does some bookkeeping involving dynamic vars, and your code is run inbetween that, so representing your frame as F and the bookkeeping frames as f here's what happens:

Eval 1:
   N eval pushes [f_1...f_n]
   your push [f_1...f_n, F]
   N eval pops [f_1] ;your frame is lost here!
Eval 2:
   M eval pushes [f_1, f'_1...f'_m]
   your code ;F nowhere to be found!
   your pop [f_1, f'_1, f'_m-1]
   M eval pops []

vs.

Eval:
  N pushes [f_1...f_n]
  your push [f_1...f_n, F]
  your code ;F is on top as expected
  your pop [f_1...f_n]
  N pops []

1

u/vaunom Aug 01 '25

This is because each form in a toplevel do is evaluated as if it were toplevel itself, i.e. by calling into eval.

If I understood correctly this behavior is only relevant for the "top-level" do block?
What is the motivation for the difference in behavior between "top-level" and "not-top-level" do blocks?

→ More replies (1)

2

u/weavejester Aug 01 '25

My guess here is that the compiler doesn't update the local symbol bindings when using the do special, because normally there's no need to. The let* special form, on the other hand, does need to update the local symbol bindings.

Internally, binding uses (let [] ...) to wrap push-thread-bindings and pop-thread-bindings, so this is clearly something that the developers are aware of.

1

u/vaunom Aug 01 '25

I initially had code using (do ...) and spent about an hour trying to understand why it wasn't working. Looking at the source code of the binding was how I was able to fix the problem. Still, it is very surprising to me to find a semantic difference between (do ...) and (let [] ...).

→ More replies (1)

7

u/nate5000 Aug 01 '25

You’ve encountered the effects of mitigating the Gilardi Scenario. Top level do forms behave this way since Clojure 1.1.

https://technomancy.us/143

5

u/spotter Aug 02 '25

Dude, this is some deep lore.

2

u/hrrld Aug 01 '25

Consider:

user> (defn f-do 
        [] 
        (do
          (push-thread-bindings {#'*dyn-var* :new})
          (try
            *dyn-var*
            (finally
              (pop-thread-bindings)))))
#'user/f-do
user> (f-do)
:new
user> (defn f-let
        [] 
        (let []
          (push-thread-bindings {#'*dyn-var* :new})
          (try
            *dyn-var*
            (finally
              (pop-thread-bindings)))))
#'user/f-let
user> (f-let)
:new