How to reload a clojure file in REPL

2019-01-12 14:17发布

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:

  • edit src/foo/bar.clj
  • close the REPL
  • open the REPL
  • (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.

8条回答
姐就是有狂的资本
2楼-- · 2019-01-12 15:01

I use this in Lighttable (and the awesome instarepl) but it should be of use in other development tools. I was having the same problem with old definitions of functions and multimethods hanging around after reloads so now during development instead of declaring namespaces with:

(ns my.namespace)

I declare my namespaces like this:

(clojure.core/let [s 'my.namespace]
                  (clojure.core/remove-ns s)
                  (clojure.core/in-ns s)
                  (clojure.core/require '[clojure.core])
                  (clojure.core/refer 'clojure.core))

Pretty ugly but whenever I re-evaluate the entire namespace (Cmd-Shift-Enter in Lighttable to get the new instarepl results of each expression), it blows away all old definitions and gives me a clean environment. I was tripped up every few days by old definitions before I started doing this and it has saved my sanity. :)

查看更多
Root(大扎)
3楼-- · 2019-01-12 15:05

One liner based on papachan's answer:

(clojure.tools.namespace.repl/refresh)
查看更多
登录 后发表回答