为什么这个宏不能执行?
我有以下愚蠢的测试:
(define-syntax a
(lambda (stx)
(syntax-case stx ()
[(k e s) #'(let ((show display)) (e s))])))
(a show "something")
为什么这不起作用? (DrRacket 中显示的错误是 expand: unbound identifier in module in: show
。
但是,以下内容可以工作:
(define-syntax a
(lambda (stx)
(syntax-case stx ()
[(k e s)
(with-syntax ((show (datum->syntax #'k 'show)))
#'(let ((show display)) (e s)))])))
(a show "something")
那么为什么?
I have the following dumb test:
(define-syntax a
(lambda (stx)
(syntax-case stx ()
[(k e s) #'(let ((show display)) (e s))])))
(a show "something")
Why can't this work? (The error shows in DrRacket is expand: unbound identifier in module in: show
.
However, The following can work:
(define-syntax a
(lambda (stx)
(syntax-case stx ()
[(k e s)
(with-syntax ((show (datum->syntax #'k 'show)))
#'(let ((show display)) (e s)))])))
(a show "something")
Then WHY?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我假设您在阅读 博客文章我在早期答案中提到过——但是那博客文章准确地解释了这个问题。具体来说,您的第一个示例有两个不同的
show
标识符,一个由宏绑定,另一个不同来自顶级使用(并且未绑定)。 OTOH,在第二种情况下,您将使用用户代码的词汇上下文创建一个show
。Um, I assume that you're trying this out after reading the blog post I mentioned in an earlier answer -- but that blog post is explaining exactly this issue. Specifically, your first example has two different
show
identifiers, one that is bound by the macro, and a different one that is coming from the toplevel use (and is unbound). OTOH, in the second case you're creating ashow
with the lexical context of the user code.