Reading the clojure API for import I see that :import in the ns macro is preferred to import, however when i'm coding using swank/slime/emacs, I can't c-x c-e the (ns .. ) s-expression to get the deps into the repl, but using (import ...) I can.
Whats the reason why :import is preferred over import, and is there fast way to import the deps from a (ns ...) s-expr from my .clj file to the repl? (Same question can be generalized to :use and :refer.. thanks)
Here is my preferred workflow:
Now your Slime REPL should be in the namespace you're working on, and you can add to the ns
declaration at the top and just C-c C-k as you change things (including your ns
imports).
I'm not sure why c-x c-e wouldn't work, but C-c C-c on the ns expression does work correctly as long as the namespace already exists.
The (ns ... ) form is preferred since your code will read more easily. All the namespace declarations will be collected at the top of the file. You see this enforced by the compiler in languages like Java. Also, the containing macro ns removes the need for you to quote symbols. The same can be said of use, import, refer, etc.
I think that the C-x C-e
slime short-cut will send the piece of code to the connected swank server, and have it evaluated. For example the form:
(ns my.test
(:require [clojure.contrib.logging :as log])
(:import [java.io File]))
will create a new namespace called my.test which includes contrib logging and java.io File. It will not change the namespace of the repl. To do that press C-c M-p
from the file you are editing, and you will be prompted with the name of the namespace of that file to switch to (unless you are already in that namespace). Press enter to select. C-c C-z
should switch to the repl. Now instead of the user=>
prompt, you should see my.test=>
, indicating that you are in that namespace.
The workflow I have set-up is to compile the whole file on save, using:
(defun ed/clojure-compile-on-save (&optional args)
"Compile with slime on save"
(interactive)
(if (and (eq major-mode 'clojure-mode)
(slime-connected-p))
(slime-compile-and-load-file)))
(add-hook 'after-save-hook 'ed/clojure-compile-on-save)
That way, when ever I save the file, it gets compiled and loaded by the swank server, and I use the repl for experiments in the namespace I'm working on.
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