我需要类似于 clojure/core
的 when-let
的 when-not-empty-let
宏.所以,我刚刚添加了对 when-let
macro from clojure's source code 的 not-empty
调用:
(defmacro when-not-empty-let
"bindings => binding-form test
When test is not empty, evaluates body with binding-form bound to the value of test"
[bindings & body]
(.log js/console "check")
(let [form (first bindings) tst (second bindings)]
`(let [temp# ~tst]
(when (not-empty temp#)
(let [~form temp#]
~@body)))))
(也将 (bindings 0)
替换为 (first bindings)
,否则无法编译)
我按以下方式使用它:
(defn something
[]
(when-not-empty-let [foo ["foo"]]
(.log js/console foo)))
(something)
我得到以下输出:
undefined
check
我做错了什么?
使用 Clojure v1.9.0 构建,ClojureScript:v1.10.126,lein-cljsbuild:v1.1.7
在 Ubuntu 下的 Chrome v59.0.3071.115 中测试。
UPD: 重现问题的 jsbin(至少对我而言):https://jsbin.com/liluwer/1/edit?js,output
在浏览器的开发者工具控制台中查看问题的输出。
最佳答案
来自 ClojureScript docs :
There is a strict rule for when you can use defmacro -- you can only use it in what we call a macro namespace, effectively forcing you to separate your compile time and runtime code.
错误是我试图在同一个命名空间中测试宏。
关于clojure - 为什么我的 clojurescript 宏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465567/