Clojure import versus: import

Reading the clojure API for import I can see that: import in the ns macro is preferable to import, however, when I code using swank / slime / emacs, I cannot cx ce (ns ..) s-expression to get deps in repl, but using (import ...) I can.

What is the reason: import is preferable to import, and is there a quick way to import a depot from a (ns ...) s-expr file from my .clj file into repl? (The same question can be generalized to: use and: refer .. thanks)

+5
source share
4 answers

Here is my preferred workflow:

  • Start Swank / Slime
  • Open the file I want to work in Emacs
  • C-c C-k
  • ,, i, Enter

Slime REPL , , ns C-c C-k ( ns).

+5

, c-x c-e , C-c C-c ns , .

+2

(ns...) , . . , , Java. , ns . , , ..

, slime C-x C-e swank . , :

(ns my.test
   (:require [clojure.contrib.logging :as log])
   (:import [java.io File]))

, my.test, log java.io File. repl. C-c M-p , , ( ). enter . C-c C-z repl. user=> my.test=>, , .

, , , :

(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)

, , swank, repl , .

+2

: ns , , , , . .

, , ns C-x C-e, , SLIME. sexp, SLIME . , evaling (import 'java.io.File) ns .

evaling an (ns my.namespace declaration (: import java.io.File) changes the namespace specified in the declaration, but it does not change the value ns. If you do not go into this namespace with (in -ns' my.namespace), you will not see import

+1
source

All Articles