两个文件
types.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和书籍中的一些最简单的例子之外,我找不到关于“记录”的更多用法。
任何帮助,非常感谢!
答案 0 :(得分:9)
defrecord在以当前命名空间命名的包中创建一个java类。 (ProductPrice。...)是对该类型的构造函数的调用;这是java interop - 不是简单的函数调用。
除非明确导入或指定完整的包名,否则不能引用java.lang之外定义的类或当前命名空间。这包括调用它的构造函数。
因此,要解决此问题,您需要导入Price和ProductPrice。
(ns test.core (:import [test.types Price]))
(Price. ...)
或调用完整的类+包名称:
(test.types.Price. ...)