What is the preferred way of reloading functions defined in a Clojure file without having to restart the REPL. Right now, in order to use the updated file I have to:
The best answer is:
(require 'my.namespace :reload-all)
This will not only reload your specified namespace, but will reload all dependency namespaces as well.
Documentation:
require
Or
(use 'your.namespace :reload)
As soon as (use 'foo.bar)
works for you, it means that you have foo/bar.clj or foo/bar_init.class on your CLASSPATH. The bar_init.class would be an AOT-compiled version of bar.clj. If you do (use 'foo.bar)
, I'm not exactly sure if Clojure prefers class over clj or the other way round. If it would prefer class files and you have both files, then it's clear that editing the clj file and then reloading the namespace has no effect.
BTW: You don't need to load-file
before the use
if your CLASSPATH is set properly.
BTW2: If you need to use load-file
for a reason, then you can simply do it again if you edited the file.
One liner based on papachan's answer:
(clojure.tools.namespace.repl/refresh)
There is also an alternative like using tools.namespace, it's pretty efficient:
user=> (use '[clojure.tools.namespace.repl :only (refresh)])
user=> (refresh)
:reloading (namespace.app)
:ok
Try load-file again?
If youre using an IDE, there's usually a keyboard shortcut to send a code-block to the REPL, thus effectively re-defining the associated functions.