12
votes

I'm trying to use dotrace function of clojure.tools.trace namespace.

(dotrace [my-function] (my-function 5))

But I got this error:

IllegalStateException Can't dynamically bind non-dynamic var: my-ns/my-function clojure.lang.Var.pushThreadBindings (Var.java:339)

What does it mean? why I'm getting it?

(I'm using Clojure 1.3)

1
Can you also specify the code for my-function? - Ankur

1 Answers

20
votes

I think you need to declare your function as being dynamically bound, e.g.

(def ^:dynamic my-function
   (fn [x] .....))

The reason for that is because if you don't explicitly ask for a dynamic var, Clojure (1.3 upwards) will make it non-dynamic as a performance optimization.