Clojure需要命名空间:"不知道如何从:clojure.lang.Keyword创建ISeq"

Ixx*_*Ixx 5 clojure

我正在尝试将代码分成2个文件,每个文件都有自己的命名空间.遵循本教程.

但是我得到了这个错误:线程"main"中的异常java.lang.IllegalArgumentException:不知道如何创建ISeq:clojure.lang.Keyword

我认为这是因为包含的命名空间无法正确识别.

主文件:

(ns mytest2.handler
  (:use compojure.core)
  (:require [compojure.handler :as handler]
            [compojure.route :as route]
            [mytest2.views :as foo] ;<-- line causing error
            [hiccup.core :refer (html)])
  )



(defn layout [title & content]
  (html
   [:head [:title title]]
   [:body content]))

(defn main-page []
  (layout "My Blog"
   [:h1 "My Blog"]
   [:p "Welcome to my page"]))

(defroutes app-routes
  (GET "/" [] (main-page))
  (route/resources "/")
  (route/not-found "Not Found"))

(def app
  (handler/site app-routes))

;    (println (seq (.getURLs (java.lang.ClassLoader/getSystemClassLoader))))
Run Code Online (Sandbox Code Playgroud)

第二档:

(ns mytest2.views
  :require [hiccup.core :refer (html)]
  )

(defn layout [title & content]
  (html
   [:head [:title title]]
   [:body content]))

(defn main-page []
  (layout "My Blog"
   [:h1 "My Blog"]
   [:p "Welcome to my page"]))
Run Code Online (Sandbox Code Playgroud)

(注意我在mytest2.handler中复制mytest2.views中的函数进行测试.它们不应该在mytest2.handler中).

文件路径:

/mytest2/src/mytest2/handler.clj

/mytest2/src/mytest2/views.clj

(其中第一个mytest2是项目的名称,第二个是路径的一部分 - 由lein自动创建).

正如您在第一个文件中看到的那样,我打印了类路径来验证是否包含/ mytest2/src/mytest2 /,是的.

小智 19

尝试:refer :all在Clojurescript中使用时收到了同样的错误,这显然违反了规则.

  • 这是我遇到的问题,而不是缺少的括号. (2认同)
  • 经过半个小时的调试,你的用户名完全概括了我的感受 (2认同)

n2o*_*n2o 7

您错过了原始代码中的一些括号

;; wrong
(ns mytest2.views
  :require [hiccup.core :refer [html]])
Run Code Online (Sandbox Code Playgroud)

只缺少一对括号.在主文件中执行此操作:

;; Done right!
(ns mytest2.views
  (:require [hiccup.core :refer [html]]))
Run Code Online (Sandbox Code Playgroud)

我不熟悉Compojure所以我不知道你需要什么.但是你需要添加括号:require.