Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure DEF宏中的deftype带有元数据_Clojure - Fatal编程技术网

Clojure DEF宏中的deftype带有元数据

Clojure DEF宏中的deftype带有元数据,clojure,Clojure,假设我让deftype从下一个角度看: (deftype ^{A: a} Name []) 我要定义宏类型: (defmacro dotype [name] `(deftype ^{A :a} ~name [])) 但是,我正在丢失有关元数据的信息 (macroexpand-1 '(dotype T)) ;> (clojure.core/deftype T []) 我尝试使用vary meta的技巧来避免在宏中使用^{}。不幸的是,deftype不支持IObj接口(不支持元数据)

假设我让deftype从下一个角度看:

(deftype ^{A: a} Name [])
我要定义宏类型:

(defmacro dotype [name]
  `(deftype ^{A :a} ~name []))
但是,我正在丢失有关元数据的信息

(macroexpand-1 '(dotype T))
;> (clojure.core/deftype T [])
我尝试使用
vary meta
的技巧来避免在宏中使用^{}。不幸的是,
deftype
不支持IObj接口(不支持元数据),我所有的尝试都没有成功

请建议实现此宏的方法。谢谢

这对我很有用:

(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*
来启用元数据,或者使用
(set!*print meta*true)
在REPL中设置元数据,但设置为永久在REPL中会将大量无趣的信息打印到屏幕上,因此最好避免(!)