debuggingclojureinlineread-eval-print-loopvscode-calva

Clojure: capture runtime value of function arg, to use in REPL


Problem

My web front-end calls back-end queries with complex arguments. During development, to avoid time-consuming manual replication of those arguments, I want to capture their values in Vars, which can be used in REPL.

Research

This article shows that inline def is a suitable solution, but I couldn't make it work. After a call from the front-end happened, the Var remained unbound.

I launched the backend with REPL through VS Code + Calva, with the following code:

(defn get-analytics-by-category [params]
  (def params params)
  ...)

And here's the evaluation of the Var in the REPL:

#object[clojure.lang.Var$Unbound 0x1298f89e "Unbound: #'urbest.db.queries/params"]

Question

Why the code above didn't bound value of the argument to the Var? Is there another solution?


Solution

  • The best way that I found is to use scope-capture library. It captures all the local variables with addition of 1 line in a function, and then with another 1-liner you can define all those variables as global, which allows you to evaluate in REPL any sub-expression in the function using runtime values.

    If you ever spent a lot of time reproducing complex runtime values, I strongly recommend watching their 8-min demo.


    My issue with inline-def was likely caused by reloading namespace after the Var was bound to a value. After restarting VS Code and carefully doing everything again, the issue went away.