Macros 如何向Clojure中的宏添加第二个参数?
我有以下带有1个参数的Clojure包装宏:Macros 如何向Clojure中的宏添加第二个参数?,macros,clojure,with-statement,Macros,Clojure,With Statement,我有以下带有1个参数的Clojure包装宏: (defmacro with-init-check "Wraps the given statements with an init check." [body] `(if (initialized?) ~body (throw (IllegalStateException. "GeoIP db not initialized.")))) 我想向其中添加ip版本,以便检查是否只初始化了:IPv6或:IPv4。但是,当我尝试此操作时,该参数未
(defmacro with-init-check
"Wraps the given statements with an init check."
[body]
`(if (initialized?)
~body
(throw (IllegalStateException. "GeoIP db not initialized."))))
我想向其中添加ip版本
,以便检查是否只初始化了:IPv6
或:IPv4
。但是,当我尝试此操作时,该参数未被传递:
(defmacro with-init-check
"Wraps the given statements with an init check."
[ip-version body]
`(if (initialized? ip-version)
~body
(throw (IllegalStateException. "GeoIP db not initialized."))))
当我这样使用它时,我在“if let[location…”行中得到“no-this var”:
如何将
ip版本
获取到初始化功能?用~
将其解压缩:
(defmacro with-init-check
"Wraps the given statements with an init check."
[ip-version body]
`(if (initialized? ~ip-version) ; unquoted with ~
~body
(throw (IllegalStateException. "GeoIP db not initialized."))))
从docstring推断,您可能还希望允许多个表达式体,并在do
表达式中取消对它们的拼接:
(defmacro with-init-check
"Wraps the given statements with an init check."
[ip-version & body] ; multi-expression bodies with &
`(if (initialized? ~ip-version)
(do ~@body) ; unquote-spliced with ~@
(throw (IllegalStateException. "GeoIP db not initialized."))))
用~
将其解压缩:
(defmacro with-init-check
"Wraps the given statements with an init check."
[ip-version body]
`(if (initialized? ~ip-version) ; unquoted with ~
~body
(throw (IllegalStateException. "GeoIP db not initialized."))))
从docstring推断,您可能还希望允许多个表达式体,并在do
表达式中取消对它们的拼接:
(defmacro with-init-check
"Wraps the given statements with an init check."
[ip-version & body] ; multi-expression bodies with &
`(if (initialized? ~ip-version)
(do ~@body) ; unquote-spliced with ~@
(throw (IllegalStateException. "GeoIP db not initialized."))))
以下是联机代码,您可以查看上下文:以下是联机代码,您可以查看上下文: