为什么 clojurescript 宏不能用 clojurescript 编写? [英] Why clojurescript macros can't be written in clojurescript?

查看:20
本文介绍了为什么 clojurescript 宏不能用 clojurescript 编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然 clojure 和 clojurescript 的功能基本相同(除了明显的差异),但宏不是.当我想要 clojurescript 中的宏时,我必须在 clojure 中编写它并使用 require-macros 来要求它.这是javascript的技术限制还是设计决定?为什么两者不能相同?

While clojure and clojurescript features are basically the same (apart from obvious differences), macros are not. When I want a macro in clojurescript I have to write it in clojure and require it with require-macros. Is that a technical limitation of javascript or just a design decision? Why can't both be the same?

推荐答案

来自 ClojureScript: Up and Running 作者Stuart Sierra 和 Luker VanderHart,第 69 页:

From ClojureScript: Up and Running by Stuart Sierra and Luker VanderHart, page 69:

在编译过程中应用宏.他们不存在在运行时.因为ClojureScript 编译器是在 Clojure 中实现的,必须编写 ClojureScript 宏在 Clojure 中,而不是 ClojureScript.幸运的是,Clojure 和 ClojureScript 几乎相同在操作数据结构时,两种语言之间的切换是不难.

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. Fortunately, Clojure and ClojureScript are almost identical when it comes to manipulating data structures, so switching between the two languages is not difficult.

这意味着宏代码是在 Clojure 世界中执行的,而不是在浏览器中执行,所以它必须用普通的 Clojure 编写.

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.

这篇关于为什么 clojurescript 宏不能用 clojurescript 编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆