我正在尝试定义一个将创建宏的函数,当我尝试动态提供宏的名称时遇到了问题。这是示例我面临的问题的缩小代码:
(defn create-times-macro [n]
(defmacro thatManyTimes [a]
`(* ~n ~a)))
(create-times-macro 2)
(thatManyTimes 3) ;; evals to 6
到现在为止还挺好。现在说我想提供宏的名称作为参数:
(defn create-times-macro [macroName n]
(defmacro macroName [a]
`(* ~n ~a)))
(create-times-macro (symbol "multiplyBy") 3)
(multiplyBy 3) ;; fails with unable to resolve symbol multiplyBy
(create-times-macro "multiplyBy" 3)
(multiplyBy 3) ;; same failure