defmacro中的deftype与元数据

时间:2017-04-06 00:09:45

标签: clojure

让我们假设我有下一个方向的deftype:

(deftype ^{A: a} Name [])

我想定义宏生成deftype:

(defmacro dotype [name]
  `(deftype ^{A :a} ~name []))

但是,我正在丢失有关元数据的信息。

(macroexpand-1 '(dotype T))
;> (clojure.core/deftype T [])

我尝试使用vary-meta的技巧来避免在宏中使用^ {}。不幸的是,deftype不支持IObj接口(不支持元数据),并且我的所有尝试都不起作用。

请建议实施此宏的方法。谢谢!

1 个答案:

答案 0 :(得分:2)

这对我有用:

vba

注意:默认情况下不打印元数据,因此您必须通过绑定(deftype A []) (defmacro dotype [name] `(deftype ~(with-meta name {:tag A}) [])) (binding [*print-meta* true] (prn (macroexpand-1 '(dotype T)))) ; user=> (clojure.core/deftype ^user.A T []) 启用它,如上所示,或者使用*print-meta*将其设置在REPL中,但永久设置为REPL会在屏幕上打印大量不感兴趣的信息,所以最好避免(!)。