Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a "def" to not reload in clojure?

Tags:

clojure

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?

like image 820
yazz.com Avatar asked Jan 04 '11 22:01

yazz.com


2 Answers

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.

like image 129
Arthur Ulfeldt Avatar answered Nov 15 '22 11:11

Arthur Ulfeldt


clojure.core/defonce

like image 32
MayDaniel Avatar answered Nov 15 '22 11:11

MayDaniel