两个文件
types.clj:
(ns test.types)
(defrecord Price [date price])
(defrecord ProductPrice [name prices])
Run Code Online (Sandbox Code Playgroud)
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)))))))
Run Code Online (Sandbox Code Playgroud)
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))))))))
Run Code Online (Sandbox Code Playgroud)
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)))))))
Run Code Online (Sandbox Code Playgroud)
core.clj(java.lang.IllegalStateException: - > ProductPrice已经引用:#'test.types/ - > ProductPrice in namespace: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)))))))
Run Code Online (Sandbox Code Playgroud)
我对这些例外感到困惑.除了clojure.org和书籍中的一些最简单的例子之外,我找不到关于"记录"的更多用法.
任何帮助,非常感谢!
defrecord在以当前命名空间命名的包中创建一个java类.(ProductPrice....)是对该类型的构造函数的调用; 这是java interop - 不是普通的函数调用.
除非明确导入它或指定完整的包名,否则不能引用在java.lang之外定义的类或当前命名空间.这包括调用它的构造函数.
因此,要解决问题,您需要导入Price和ProductPrice.
(ns test.core (:import [test.types Price]))
(Price. ...)
Run Code Online (Sandbox Code Playgroud)
或者调用完整的类+包名称:
(test.types.Price. ...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2011 次 |
| 最近记录: |