我想要一個宏來定義返回它們被調用的形式的函數。 (func 1 (a b))
返回(func 1 (a b))
。我也想允許這些函數的輸入驗證,以確保我沒有引入任何錯誤。 (這些表格稍後會被評估,但該代碼尚未寫入。)在宏中定義一個函數:不能使用限定名作爲參數
雖然我不斷收到此錯誤。
(defmacro defecho
"Echo function call after asserting a few things about the input"
([f] `(defecho ~f nil nil))
([f assertions] `(defecho ~f assertions nil))
([f assertions assert-failed-message]
`(defn ~f [& body] ; define a function
~(when-not (nil? assertions) ; if given a function for input validation
`(assert (~assertions body) ; define the function to assert this as true
~assert-failed-message)) ; with a given error message
(conj body (quote ~f))))) ; return the (f [email protected]) list
(defecho my-test
#(< 2 (count %))
"Must be greater than zero")
Unhandled clojure.lang.Compiler$CompilerException Error compiling: /private/var/...228.clj:1:1 Can't use qualified name as parameter: my-test/body
Caused by java.lang.RuntimeException Can't use qualified name as parameter: my-test/body
您還可以使用'body#'語法自動gensym名稱。 – drnewman
@drnewman由於示例在兩個不同的語法引用上下文中使用'body#',因此會生成不同的符號。 –
Leon Grapenthin,你說得對,謝謝你的糾正 – drnewman