How to reload a clojure file in REPL
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:
src/foo/bar.clj
(load-file "src/foo/bar.clj")
(use 'foo.bar)
In addition, (use 'foo.bar :reload-all)
does not result in required effect, which is evaluating the modified bodies of functions and returning new values, instead of behaving as the source haven't changed at all.
或者(use 'your.namespace :reload)
还有一种方法就像使用tools.namespace一样,它非常高效:
user=> (use '[clojure.tools.namespace.repl :only (refresh)])
user=> (refresh)
:reloading (namespace.app)
:ok
Reloading Clojure code using (require … :reload)
and :reload-all
is very problematic:
If you modify two namespaces which depend on each other, you must remember to reload them in the correct order to avoid compilation errors.
If you remove definitions from a source file and then reload it, those definitions are still available in memory. If other code depends on those definitions, it will continue to work but will break the next time you restart the JVM.
If the reloaded namespace contains defmulti
, you must also reload all of the associated defmethod
expressions.
If the reloaded namespace contains defprotocol
, you must also reload any records or types implementing that protocol and replace any existing instances of those records/types with new instances.
If the reloaded namespace contains macros, you must also reload any namespaces which use those macros.
If the running program contains functions which close over values in the reloaded namespace, those closed-over values are not updated. (This is common in web applications which construct the "handler stack" as a composition of functions.)
The clojure.tools.namespace library improves the situation significantly. It provides an easy refresh function that does smart reloading based on a dependency graph of the namespaces.
myapp.web=> (require '[clojure.tools.namespace.repl :refer [refresh]])
nil
myapp.web=> (refresh)
:reloading (myapp.web)
:ok
Unfortunately reloading a second time will fail if the namespace in which you referenced the refresh
function changed. This is due to the fact that tools.namespace destroys the current version of the namespace before loading the new code.
myapp.web=> (refresh)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: refresh in this context, compiling:(/private/var/folders/ks/d6qbfg2s6l1bcg6ws_6bq4600000gn/T/form-init819543191440017519.clj:1:1)
You could use the fully qualified var name as a workaround for this problem but personally I prefer not having to type that out on each refresh. Another problem with the above is that after reloading the main namespace the standard REPL helper functions (like doc
and source
) are no longer referenced there.
To solve these issues I prefer to create an actual source file for the user namespace so that it can be reliably reloaded. I put the source file in ~/.lein/src/user.clj
but you can place in anywhere. The file should require the refresh function in the top ns declaration like this:
(ns user
(:require [clojure.tools.namespace.repl :refer [refresh]]))
You can setup a leiningen user profile in ~/.lein/profiles.clj
so that location you put the file in is added to the class path. The profile should look something like this:
{:user {:dependencies [[org.clojure/tools.namespace “0.2.7”]]
:repl-options { :init-ns user }
:source-paths [“/Users/me/.lein/src”]}}
Note that I set the user namespace as the entry point when launching the REPL. This ensures that the REPL helper functions get referenced in the user namespace instead of the main namespace of your application. That way they won't get lost unless you alter the source file we just created.
Hope this helps!
链接地址: http://www.djcxy.com/p/38580.html上一篇: Emacs中的Clojure文档