views:

162

answers:

2

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?

+4  A: 

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 imported 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.)

Michał Marczyk
A: 

Maybe the "dirty" way is simply switch to the namespace of the buffer in the repl with (ns 'example.code) . Dirty cause you mix the buffer definitions with repl ones. I think the Marczyk answer is the right one.

jneira
Also in-ns is the "Right Way" to switch namespaces, not ns. ns is supposed to be called only once to setup the namespace with all its require's, use's and import's. When this is done, in-ns is used to switch the namespace.
kotarak
you are right, i miss the in-ns, more suitable for the repl
jneira