繁体   English   中英

在宏中模拟“let”语句

[英]Mimic `let' statement in a macro

在以下目录结构中:

.
├── a
│  ├── 1
│  ├── 2
│  ├── 3
│  └── 4
├── b
│  ├── 5
│  ├── 6
│  ├── 7
│  └── 8
├── c
├── d
└── test.hy

以下代码打印了错误的路径:

(eval-and-compile (import os) (import pathlib [Path]))

(defmacro with-cwd [dir #* body]
          `(let [ cwd (.cwd Path) ]
               (try (.chdir os ~dir)
                    ~@body
                    (finally (.chdir os cwd)))))

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

(setv a (/ (. (Path __file__) parent) "a"))

(let-cwd a [ b (/ a.parent "b") ] (print f"Hello from {(.cwd Path)}!\n") (print (.resolve b)))

它应该打印以下内容:

Hello from /home/shadowrylander/with-cwd-test/a!

/home/shadowrylander/with-cwd-test/b

虽然它正在打印:

Hello from /home/shadowrylander/with-cwd-test/a!

/home/shadowrylander/with-cwd-test/a/b

为什么b在做(quasiquote (let ~vars (with-cwd ~dir ~@body)))时没有正确分配?

这看起来像一个错误( https://github.com/hylang/hy/issues/2318 ),尽管它与__file__有关并且与let或宏无关。 请记住,当您遇到麻烦时,请尽可能简化有问题的代码,以便弄清楚发生了什么。

暂无
暂无

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

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