简体   繁体   English

在另一个宏中需要来自同一文件的宏

[英]Require macros from the same file in another macro

In the following code, given that hy seem to give a NameError when not explicitly requiring a macro's dependent macros, how do I require a macro in the same file in another macro?在下面的代码中,鉴于hy似乎在未明确需要宏的依赖宏时给出NameError ,我如何在另一个宏中需要同一文件中的宏?

(defmacro with-cwd [#* body] `(do ~@body))
(defmacro let-cwd [vars #* body] `(let ~vars (with-cwd ~@body)))

In a package called oreo , in a file called oreo.hy , I tried the following methods using (require oreo.oreo [with-cwd]) , and none worked:在名为oreo oreo.hy文件中,我使用(require oreo.oreo [with-cwd])尝试了以下方法,但均未奏效:

(defmacro let-cwd [vars #* body] (require oreo.oreo [with-cwd]) `(let ~vars (with-cwd ~@body)))

(defmacro let-cwd [vars #* body] `(let ~vars (require oreo.oreo [with-cwd]) (with-cwd ~@body)))

(defmacro let-cwd [vars #* body] `(let ~vars ~(require oreo.oreo [with-cwd]) (with-cwd ~@body)))

(defmacro let-cwd [vars #* body] `(do (require oreo.oreo [with-cwd]) (let ~vars (with-cwd ~@body))))

(defmacro let-cwd [vars #* body] `(do ~(require oreo.oreo [with-cwd]) (let ~vars (with-cwd ~@body))))

UPDATE: Upon request, I have modified the code to the point where the original problem still remains, but the code itself is significantly shorter.更新:根据要求,我已将代码修改为原始问题仍然存在的程度,但代码本身要短得多。

The fourth version works, as noted by Kodiologist in their comment here ;正如Kodiologist此处的评论中所指出的,第四个版本有效; the final versions of two test files are therefore:因此,两个测试文件的最终版本是:

;; a.hy
(defmacro with-cwd [#* body] `(do ~@body))
(defmacro let-cwd [vars #* body] `(do (require oreo.oreo [with-cwd]) (let ~vars (with-cwd ~@body))))

And:和:

;; b.hy
(require a [let-cwd])
(let-cwd [ string "Hello!" ] (print string))

The second file then outputs Hello .然后第二个文件输出Hello

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

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