Desidero che una macro definisca funzioni che restituiscono il modulo in cui sono stati chiamati, ad es. (func 1 (a b))
restituisce (func 1 (a b))
. Voglio anche consentire la verifica dell'input per queste funzioni per assicurarmi che non stia introducendo alcun bug. (Questi moduli verranno successivamente valutati, ma quel codice non è stato ancora scritto.)Definizione di una funzione in una macro: impossibile utilizzare il nome qualificato come parametro
Continuo a ricevere questo errore, però.
(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
È inoltre possibile utilizzare la sintassi 'body #' per auto gensym il nome. – drnewman
@drnewman Poiché l'esempio usa 'body #' in due diversi contesti di citazione della sintassi, sarebbero generati simboli diversi. –
Leon Grapenthin, hai ragione, grazie per la correzione – drnewman