I am just starting out learning Clojure and Emacs. I have got Clojure Box for windows running and I would like to be able to write code in a buffer then run it in the REPL without haveing to call
(use 'example.code)
all the time. I know about C-c C-k but it doesn't reload the namespace. If i use
(in-ns 'example.code)
to change namespace in the repl it works. What is the right way to do this?
in-ns
is one of the right ways.
The way which feels most "right" to me is to (require '[example.code :as ec])
and work in the user
namespace at the REPL; that way my throwaway experimental state stays in user
and ec/foo
is convenient enough to me (and it makes it obvious where foo
is supposed to come from). You can always say (require :reload-all 'example.code)
(same works with use
) to force recompilation.
Also, here's a function to remove (from the current namespace) all mappings pulled in from a given namespace with use
:
(defn unuse [ns]
(doseq [[n v] (ns-refers *ns*)]
(if (= (.. v ns name) ns)
(ns-unmap *ns* n))))
On top of that you could build
(defn reuse [ns]
(unuse ns)
(remove-ns ns)
(use :reload-all ns))
and say (reuse 'example.code)
to get something close to a fresh start with your namespace. (Note that 1.2 new features such as deftype
& defrecord
introduce some complexities... In particular, unuse
has no effect on import
ed class -- this includes records and deftype
-created types. :reload-all
still causes the deftype
et al. forms to be recompiled, but I remember hitting weird cases where this didn't seem to be enough... Possibly my error, possibly some arcane aspect of these features I haven't yet fully explored.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With