簡體   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