clojure - 为什么我的 clojurescript 宏不起作用

标签 clojure macros lisp clojurescript leiningen

我需要类似于 clojure/corewhen-letwhen-not-empty-let 宏.所以,我刚刚添加了对 when-let macro from clojure's source codenot-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/

相关文章:

lisp - 未实习符号的奇怪行为

macros - 获取 Haxe 宏中对象的类型

c++ - SOMTEMPLATE_VR_API 是什么?

list - Lisp 遍历函数中的列表

maven - 如何打包 leiningen 项目以重新编译并包含所有库? [对于没有互联网连接的用户]

macros - clojure - 以非普通方式扩展类型

installation - 如何在 Debian 上安装/升级 Lisp 库

clojure-protobuf - 无法运行程序 [path protoc](在目录 "resources/proto"中): error=2, 没有这样的文件或目录

api - 获取 Clojure 库的 API 文档

clojure - 将列表转换回 map map