I have a def in a namespace which I want initialised once when the application is started, but every time the namespace containing the "def" is "used" and :reloaded from another namespace then the "def" is re-evaluated. How can I prevent the "def" from being re-evaulated during this reload?
defonce
will get you close though it will still be defined again if the namespace is :reloaded because (use 'my.namespace :reload)
first throws out the old root binding.
Defonce is a macro that checks if the var already has a root binding in the given namespace and then only defines it if it does not. You can use this to solve your problem by putting this special def into its own namespace with an (in-ns ... )
to protect it from being wiped out by a reload. then you can reload the 'main' namespace as often as you want and the code should only be defined once.
clojure.core/defonce
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With