简体   繁体   English

为什么clojurescript宏不能用clojurescript写?

[英]Why clojurescript macros can't be written in clojurescript?

While clojure and clojurescript features are basically the same (apart from obvious differences), macros are not. 虽然clojure和clojurescript功能基本相同(除了明显的差异),但宏不是。 When I want a macro in clojurescript I have to write it in clojure and require it with require-macros. 当我想要一个clojurescript中的宏时,我必须在clojure中编写它并且需要它与require-macros。 Is that a technical limitation of javascript or just a design decision? 这是javascript的技术限制还是仅仅是设计决策? Why can't both be the same? 为什么不能两者都一样?

From ClojureScript: Up and Running by Stuart Sierra and Luker VanderHart, page 69: 来自ClojureScript: Stuart Sierra和Luker VanderHart的启动和运行 ,第69页:

Macros are applied during the compilation process. 在编译过程中应用宏。 They do not exist at runtime. 它们在运行时不存在。 Because the ClojureScript compiler is implemented in Clojure, ClojureScript macros must be written in Clojure, not ClojureScript. 由于ClojureScript编译器是在Clojure中实现的,因此ClojureScript宏必须用Clojure编写,而不是用ClojureScript编写。 Fortunately, Clojure and ClojureScript are almost identical when it comes to manipulating data structures, so switching between the two languages is not difficult. 幸运的是,Clojure和ClojureScript在操作数据结构时几乎完全相同,因此在两种语言之间切换并不困难。

What that means is that macro code gets executed in Clojure world and not in the browser, so it must be written in plain Clojure. 这意味着宏代码在Clojure世界而不是在浏览器中执行,所以它必须用简单的Clojure编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM