我记得我在某处读到它不是宏,而是内置于核心语言中。像那样的东西,我不确定,因为我不记得我是从哪里读到的。那么 struct
是不是 Racket 中的宏?如果不是,为什么将它内置到核心语言中?
最佳答案
一个宏; 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
功能来定位源代码(如果有的话)。
关于racket - struct 是 Racket 中的宏吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931806/