paren`(...)宏
为了实践,我已经定义了
(defmacro quote-paren
"body -> `(body)"
[& body]
`(~@body))
它具有预期的转换(quote-paren body)
=>`(body)`。 它似乎满足一些基本测试:
user=> (macroexpand-1 `(quote-paren 3 4 5))
(3 4 5)
user=> (macroexpand-1 `(quote-paren println "hi"))
(clojure.core/println "hi")
user=> (macroexpand-1 `(quote-paren (println "hi")))
((clojure.core/println "hi"))
不过,我一直在用这个do-while宏(从这里修改)进行测试:
(defmacro do-while
[test & body]
(quote-paren loop []
~@body
(when ~test
(recur))))
(def y 4)
(do-while (> y 0)
(def y (dec y)))
但结果是
IllegalStateException Attempting to call unbound fn: #'clojure.core/unquote-splicing clojure.lang.Var$Unbound.throwArity (Var.java:43)
我不明白这一点,因为从我所看到的`quote-paren'宏可以正常工作(用〜@ body插入):
user=> (macroexpand-1
`(quote-paren loop []
(def y (dec y))
(when ~test
(recur))))
(clojure.core/loop [] (def user/y (clojure.core/dec user/y)) (clojure.core/when #<core$test clojure.core$test@1f07f672> (recur)))
但试图宏观扩张do-while
导致一个“ unbound fn
”。 我错过了些微妙的东西吗?
缺少quote-paren
之前的语法引用
user> (defmacro do-while
[test & body]
`(quote-paren loop []
~@body
(when ~test
(recur))))
#'user/do-while
然后适当扩展:
user> (macroexpand '(do-while (> y 0)
(def y (dec y))))
(loop* [] (def y (dec y)) (clojure.core/when (> y 0) (recur)))
并似乎工作:
user> (def y 4)
#'user/y
user> (do-while (> y 0)
(def y (dec y)))
nil
user>
链接地址: http://www.djcxy.com/p/11271.html
上一篇: paren `( ... ) macro