如果有一个原子:
(def a (atom {}))
配备以下 watch
(add-watch a :watcher println)
(add-watch a :watcher2 println)
有这样的函数吗?
(get-watches a)
;; => [:watcher :watcher2]
最佳答案
(atom {})
创建一个 clojure.lang.Atom
类型的对象,该对象扩展了抽象类 clojure.lang.ARef它实现了 clojure.lang.IRef界面。 IRef 声明方法 getWatches在 ARef 中实现。
解决方案如下:
(def a (atom {}))
(add-watch a :watcher println)
(println (-> a .getWatches keys))
奇怪的是clojure.core
没有get-watches
。镜像 add-watch
实现我们得到:
(defn get-watches
"Returns list of keys corresponding to watchers of the reference."
[^clojure.lang.IRef reference]
(keys (.getWatches reference)))
关于clojure - 有没有办法在 Clojure 中获取 watch 的所有按键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12813695/