有点古怪,但对我有用:
(defprotocol P (methodname [arg1]))`
(defmacro gen-type [type-name field-list proto-name proto-sig]
`(load-string (str "(deftype " ~type-name " " ~(str field-list) ~(str proto-name) ~(str proto-sig) ")")))
(gen-type "T" [f1 f2] P (methodname [arg1]))
所以我基本上只是建立一个字符串,并与读者一起评估。
没有加载字符串(仍然不太灵活,但可以完成任务):
(defmacro gentype [t f p m a b]
`(deftype ~(symbol t) ~f ~p (~m ~a ~b)))
(gentype "T3" [f1 f2] P methodname [arg1] (prn arg1))