我正在尝试创建一个函数来创建一个以另一个结构为基础的新基础,作为一个开始,我试图创建一个宏,它将创建一个与旧结构相同的新结构.我认为应该这样做的宏在下面,但是它给出了以下错误:
java.lang.Exception: Can't use qualified name as parameter: user/p1__132
Run Code Online (Sandbox Code Playgroud)
宏:
(defmacro prototype [structure obj]
`(apply struct ~structure (map #(~obj %) (keys ~obj))))
Run Code Online (Sandbox Code Playgroud)
使用示例:
(defstruct bintree :data :left :right)
(def a (struct bintree 3))
(prototype bintree a)
Run Code Online (Sandbox Code Playgroud)
在这种情况下,所需的输出将是
{:data 3 :left nil :right nil}
Run Code Online (Sandbox Code Playgroud)
作为对您的问题的评论发布的链接seth包含答案(罪魁祸首是处理匿名函数的参数的方式); 以下,使用gensym参数,应该工作:
(defmacro prototype [structure obj]
`(apply struct ~structure (map (fn [x#] (~obj x#)) (keys ~obj))))
Run Code Online (Sandbox Code Playgroud)