3

我很好奇某些宏在做什么,并试图调用(macroexpand-1)以获取更多信息。但是,对于如何扩展 ClojureScript 中的内置宏,尤其是cljs.core命名空间中的宏,我有点困惑。根据文档,ClojureScript 宏是用 Clojure 编写的,因此必须在 Clojure REPL(而不是 ClojureScript REPL)中进行测试,这是我一直在尝试的地方。

从我的 ClojureScript 项目的目录中运行lein repl,我试过这个:

=> (require 'cljs.compiler)
=> (require 'cljs.core)
=> (macroexpand-1 '(cljs.core/int 99.9))
(macroexpand-1 '(cljs.core/int 99.9))
(cljs.core/int 99.9)

为什么会返回(cljs.core/int 99.9)?基于ClojureScript 源,该宏不应该扩展为类似的东西(bit-or ~x 0)吗?

当我扩展非 ClojureScript 宏时,例如(macroexpand-1 '(when (even? 2) (println "2 is even"))),扩展似乎工作正常。

好像我在概念上遗漏了一些东西......

4

2 回答 2

5

很可能您正在使用此提交之前的 ClojureScript 版本,该版本引入了int宏。尝试添加[org.clojure/clojurescript "0.0-1835"]到您的:dependencies.

此外,虽然这在这里无关紧要,但一般来说,您应该使用 ClojureScriptmacroexpand-1而不是 Clojure 进行这样的测试:

(require '[cljs.compiler :as comp]) ; must be required before cljs.core
(require '[cljs.core :as core])     ; the macros live here
(require '[cljs.analyzer :as ana])  ; macroexpand-1 lives here

;; ClojureScript's macroexpand-1 takes an environment as its first
;; argument; here's a useful initial environment:
(ana/macroexpand-1 {:locals {} :context :expr :ns 'cljs.user} '(int 5))
;= (cljs.core/bit-or 5 0)
于 2013-07-05T19:17:58.823 回答
4

没有编译器宏int就是全部。

于 2013-07-05T15:17:10.427 回答