本文介绍了Racket 中的 struct 是一个宏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我记得我在某处读到它不是宏而是内置于核心语言中.类似的东西,我不确定,因为我已经记不起从哪里读到它了.那么 struct
是否是 Racket 中的宏?如果没有,为什么它被内置到核心语言中?
I remember I read somewhere it is not a macro and is built into the core language. Something like that, I am not sure, because I can no longer remember from where I read it. So is struct
a macro in Racket or not? If not, why is it built into the core language?
推荐答案
一个宏;struct.rkt
有
(define-syntax (struct stx)
(define (config-has-name? config)
(cond
[(syntax? config) (config-has-name? (syntax-e config))]
[(pair? config) (or (eq? (syntax-e (car config)) '#:constructor-name)
(eq? (syntax-e (car config)) '#:extra-constructor-name)
(config-has-name? (cdr config)))]
[else #f]))
(with-syntax ([orig stx])
(syntax-case stx ()
[(_ id super-id fields . config)
(and (identifier? #'id)
(identifier? #'super-id))
(if (not (config-has-name? #'config))
(syntax/loc stx
(define-struct/derived orig (id super-id) fields #:constructor-name id . config))
(syntax/loc stx
(define-struct/derived orig (id super-id) fields . config)))]
[(_ id fields . config)
(identifier? #'id)
(if (not (config-has-name? #'config))
(syntax/loc stx
(define-struct/derived orig id fields #:constructor-name id . config))
(syntax/loc stx
(define-struct/derived orig id fields . config)))]
[(_ id . rest)
(identifier? #'id)
(syntax/loc stx
(define-struct/derived orig id . rest))]
[(_ thing . _)
(raise-syntax-error #f
"expected an identifier for the structure type name"
stx
#'thing)]))))
在 Racket IDE 中,您可以使用 Open Defining File
功能来定位源代码(如果可用).
In Racket IDE, you can use the Open Defining File
function to locate the source code (if available).
这篇关于Racket 中的 struct 是一个宏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!