我想定義一個函數,將創建一個宏,並在我試圖動態提供宏的名稱時遇到問題。這裏列出了我正在面臨的問題的縮小代碼:功能,創建宏作爲參數提供的宏名稱
(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
你確定它能在JAR中工作嗎?請參閱下面的@Ankur答案。 – Blacksad 2013-02-26 23:04:02
@Blacksad:這將在JAR中工作,因爲它是宏創建宏而不是創建宏的函數。 – Ankur 2013-02-27 04:25:52
我明白了。感謝您的領導! – Blacksad 2013-02-27 05:18:26