简体   繁体   中英

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?

(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:

(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 ; 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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