5

两个文件

类型.clj:

(ns test.types)

(defrecord Price [date price])
(defrecord ProductPrice [name prices])

core.clj(没关系)

(ns test.core
  (:use [test.types])
  (:use [clojure.string :only (split)]))


(defn read-data [file]
  (let [name (subs (.getName file) 0 4)]
    (with-open [rdr (clojure.java.io/reader file)]
      (doall (map #(apply ->Price (split % #"\t")) (drop 2 (line-seq rdr)))))))

core.clj(java.lang.IllegalArgumentException:无法解析类名:ProductPrice)

(ns test.core
  (:use [test.types])
  (:use [clojure.string :only (split)]))


(defn read-data [file]
  (let [name (subs (.getName file) 0 4)]
    (with-open [rdr (clojure.java.io/reader file)]
      (ProductPrice. name (doall (map #(apply ->Price (split % #"\t")) (drop 2 (line-seq rdr))))))))

core.clj(没关系)

(ns test.core
  (:use [test.types])
  (:use [clojure.string :only (split)]))

(defrecord tProductPrice [name prices])
(defn read-data [file]
  (let [name (subs (.getName file) 0 4)]
    (with-open [rdr (clojure.java.io/reader file)]
      (tProductPrice. name (doall (map #(apply ->Price (split % #"\t")) (drop 2 (line-seq rdr)))))))

core.clj(java.lang.IllegalStateException:->ProductPrice 已经引用:#'test.types/->ProductPrice 在命名空间:test.core)

(ns test.core
  (:use [test.types])
  (:use [clojure.string :only (split)]))

(defrecord ProductPrice [name prices])
(defn read-data [file]
  (let [name (subs (.getName file) 0 4)]
    (with-open [rdr (clojure.java.io/reader file)]
      (ProductPrice. name (doall (map #(apply ->Price (split % #"\t")) (drop 2 (line-seq rdr)))))))

我对这些例外完全感到困惑。除了来自 clojure.org 和书籍的一些最简单的例子外,我找不到更多关于“记录”的用法。

任何帮助,非常感谢!

4

1 回答 1

10

defrecord 在以当前命名空间命名的包中创建一个 java 类。(ProductPrice. ...) 是对该类型的构造函数的调用;这是 java interop - 不是普通的函数调用。

您不能引用在 java.lang 或当前命名空间之外定义的类,除非您显式导入它或指定完整的包名称。这包括调用它的构造函数。

因此,要解决问题,您需要导入 Price 和 ProductPrice。

 (ns test.core (:import [test.types Price]))
 (Price. ...)

或调用完整的类+包名称:

 (test.types.Price. ...)
于 2012-04-04T10:12:52.327 回答