sku*_*uro 7 clojure environment-variables
看一下clojure.test
源代码,我发现了以下内容:
(defonce ^:dynamic
^{:doc "True by default. If set to false, no test functions will
be created by deftest, set-test, or with-test. Use this to omit
tests when compiling or loading production code."
:added "1.1"}
*load-tests* true)
Run Code Online (Sandbox Code Playgroud)
是否有任何好处或原因阻止重新定义(即使用defonce
)标记为^:dynamic
?的var ?
defonce
一般情况下不会阻止重新定义,但仅在重新加载文件时才阻止。当 var 维护某种状态或上下文时,这通常很有用。我相信此处的使用defonce
可能是库开发过程中的产物,开发人员需要在开发过程中多次重新加载文件,同时仍希望保留相同的值。
由于 var 不是指向 ref,而是直接 var,因此 using^:dynamic
是正确的选择。现在代码可以以线程本地方式使用set!
或更改该值。binding