如何在ClojureScript的cljs.core命名空间中展开宏

时间:2013-07-05 14:43:46

标签: clojure clojurescript

我很好奇某些宏正在做什么,并试图调用(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 source,该宏不应该扩展为(bit-or ~x 0)之类的内容吗?

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

似乎我在概念上遗漏了某些东西......

2 个答案:

答案 0 :(得分:5)

很可能你正在使用早于this commit的ClojureScript版本,它引入了int宏。尝试将[org.clojure/clojurescript "0.0-1835"]添加到:dependencies

另外,虽然这里不相关,但一般来说你应该使用ClojureScript的macroexpand-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)

答案 1 :(得分:4)

int没有编译器宏是全部。

相关问题