Take the 2-minute tour ×

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?

share|improve this question
add comment

2 Answers

up vote 11 down vote accepted

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.

share|improve this answer
hey, has this behaviour changed since this answer ? I tried adding a (defonce x 5) to a namespace, starting a repl, x is 5, then I changed it in the source, tried (require :reload 'test) and (use :reload 'test) and yet x is still 5 –  Hendekagon Jun 28 '13 at 4:18
add comment

Your Answer


