我尝试使用 emacs 运行一个类似于 clojure 'hello world' 的程序,但它给出了错误
No such namespace: hello
。
当我在终端中启动它时,效果很好:
u@h:~/hello-world$ clj -X hello/run
Hello world, the time is 07:52 am
u@h:~/hello-world$
但在带有苹果酒的 emacs 中则不然。
节目摘自网站:
https://clojure.org/guides/deps_and_cli#_writing_a_program
我展示了项目的树:
u@h:~/hello-world$ find |grep -v cache
.
./.clj-kondo
./src
./src/hello.clj
./.lsp
./.nrepl-port
./deps.edn
内容:
./src/hello.clj
(ns hello
(:require [java-time.api :as t]))
(defn time-str
"Returns a string representation of a datetime in the local time zone."
[instant]
(t/format
(t/with-zone (t/formatter "hh:mm a") (t/zone-id))
instant))
(defn run [opts]
(println "Hello world, the time is" (time-str (t/instant))))
./deps.edn
{:deps
{clojure.java-time/clojure.java-time {:mvn/version "1.4.2"}}}
现在我如何处理 emacs:
CTRL + x CTRL + f ~/hello-world/src/hello.clj
CTRL + c CTRL + x j j
出现:
;; Connected to nREPL server - nrepl://localhost:34699
;; CIDER 1.15.1 (Cogne), nREPL 1.2.0
;; Clojure 1.11.4, Java 17.0.12
;; Docs: (doc function-name)
;; (find-doc part-of-name)
;; Source: (source function-name)
;; Javadoc: (javadoc java-object-or-class)
;; Exit: <C-c C-q>
;; Results: Stored in vars *1, *2, *3, an exception in *e;
;; ======================================================================
;; If you’re new to CIDER it is highly recommended to go through its
;; user manual first. Type <M-x cider-view-manual> to view it.
;; In case you’re seeing any warnings you should consult the manual’s
;; "Troubleshooting" section.
;;
;; Here are a few tips to get you started:
;;
;; * Press <C-h m> to see a list of the keybindings available (this
;; will work in every Emacs buffer)
;; * Press <,> to quickly invoke some REPL command
;; * Press <C-c C-z> to switch between the REPL and a Clojure file
;; * Press <M-.> to jump to the source of something (e.g. a var, a
;; Java method)
;; * Press <C-c C-d C-d> to view the documentation for something (e.g.
;; a var, a Java method)
;; * Print CIDER’s refcard and keep it close to your keyboard.
;;
;; CIDER is super customizable - try <M-x customize-group cider> to
;; get a feel for this. If you’re thirsty for knowledge you should try
;; <M-x cider-drink-a-sip>.
;;
;; If you think you’ve encountered a bug (or have some suggestions for
;; improvements) use <M-x cider-report-bug> to report it.
;;
;; Above all else - don’t panic! In case of an emergency - procure
;; some (hard) cider and enjoy it responsibly!
;;
;; You can remove this message with the <M-x cider-repl-clear-help-banner> command.
;; You can disable it from appearing on start by setting
;; ‘cider-repl-display-help-banner’ to nil.
;; ======================================================================
;; Startup: /usr/local/bin/clojure -A:dev -Sdeps \{\:deps\ \{nrepl/nrepl\ \{\:mvn/version\ \"1.2.0\"\}\ cider/cider-nrepl\ \{\:mvn/version\ \"0.49.1\"\}\}\ \:aliases\ \{\:cider/nrepl\ \{\:main-opts\ \[\"-m\"\ \"nrepl.cmdline\"\ \"--middleware\"\ \"\[cider.nrepl/cider-middleware\]\"\]\}\}\} -M:cider/nrepl
user>
然后我输入:
(hello/run)
并得到:
user> (hello/run)
Syntax error compiling at (*cider-repl ~/hello-world:localhost:34699(clj)*:43:7).
No such namespace: hello
user>
我在 emacs 中做错了什么?
您必须首先需要命名空间才能使用它:
(require 'hello)
。
请注意,单段命名空间已被弃用,您可能会收到来自 IDE/REPL/linter/其他任何内容的投诉。您可以通过创建目录
src/hello
并将 src/hello.clj
移动到例如目录来修复它。 src/hello/main.clj
或src/hello/core.clj
。当然,您必须使用 hello.main
或 hello.core
而不仅仅是 hello
,但您可以为命名空间添加别名:(require '[hello.main :as hello])
。