如何在Clojure中找到输入的记录类型?

时间:2013-10-28 22:56:13

标签: class types clojure record

当使用defrecord声明MyRecordType时,我无法弄清楚如何执行类似(实例?MyRecordType x)的操作。记录的类型和类总是clojure.lang.PersistentArrayMap。

例如:

(defrecord MyRecord1 [data-field1 data-field2])
(defrecord MyRecord2 [data-field1])
(def x (->MyRecord1 1 2))
(def y (->MyRecord2 3))
(instance? MyRecord1 x)
 => false
(instance? MyRecord2 y)
 => false
(type x)
 => clojure.lang.IPersistentMap
(class y)
 => clojure.lang.IPersistentMap
(= (type x) (type y))
 => true

1 个答案:

答案 0 :(得分:0)

它应该这样工作。这就是你的例子在Clojure 1.5.1

上的样子
user=> (defrecord MyRecord1 [data-field1 data-field2])
user=> (defrecord MyRecord2 [data-field1])
user=> (def x (->MyRecord1 1 2))
user=> (def y (->MyRecord2 3))
user=> (instance? MyRecord1 x)
true
user=> (instance? MyRecord2 y)
true
user=> (type x)
user.MyRecord1
user=> (class y)
user.MyRecord2
user=> (= (type x) (type y))
false