当使用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
答案 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