(atom {}) creates an object of type clojure.lang.Atom , which extends the abstract class clojure.lang.ARef , which implements clojure.lang.IRef . IRef declares a getWatches method that is implemented in ARef .
Here's the solution:
(def a (atom {})) (add-watch a :watcher println) (println (-> a .getWatches keys))
It is strange that clojure.core does not have get-watches . Mirroring add-watch we get:
(defn get-watches "Returns list of keys corresponding to watchers of the reference." [^clojure.lang.IRef reference] (keys (.getWatches reference)))
Ivan Koblik
source share