What is the use of ^: dynamics on defone?

Looking at the clojure.test source code , I noticed the following:

 (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) 

Is there any use or reason to prevent overriding (i.e. using defonce ) var that is marked as ^:dynamic ?

+7
source share
1 answer

defonce does not prevent overriding at all, but only when the file is reloaded. This is usually useful when var supports some state or context. I believe that using defonce here can be an artifact from the development of the library, where the developer needs to repeatedly reload the file during development, while maintaining the same value.

Since var does not point to ref, but the direct var using ^:dynamic is the right choice. Now the code can use set! or binding to change the value in the local stream.

+4
source

All Articles