在类型/球拍中以宏的形式访问类型信息

时间:2015-04-21 15:07:59

标签: reflection racket typed-racket

如何使用typed/racket语言访问结构成员的类型?函数extract-struct-info确实提供了字段访问者列表,但没有提供其类型。

(require (for-syntax racket/struct-info
                     syntax/parse))

(define-syntax (display-struct-info stx)
  (syntax-parse stx
    [(display-struct-info name:id)
     (display (extract-struct-info (syntax-local-value #'name)))
     #'(list)]))

(struct: s ([a : Number] [b : String]))

(display-struct-info s)

输出:

(.#<syntax:12:9 struct:s> .#<syntax make-s> .#<syntax:12:9 s?> (.#<syntax:12:9 s-b> .#<syntax:12:9 s-a>) (#f #f) #t)'()

更一般地说,如何访问类型/球拍的类型,例如获取union type (U Number String 'foo 'bar)中的类型列表,或查看polymorphic type的参数?

我只对宏扩展时访问感兴趣,运行时访问。

相关:Get the type information in macros(Common Lisp的相同问题)

2 个答案:

答案 0 :(得分:3)

来自优秀的权威(Typed Racket的开发者)[来自freenode上的#racket irc频道]:

mithos28: ... the brief answer is that types are not available until after macro expansion
soegaard: Quoting: "I am interested only in macro-expansion-time access, not run-time access."
soegaard: So only after - or also during?
mithos28: only after. #%module-begin from TR does an local expand of the body and then once that returns traverses it and checks types and generates contracts/optimizes

答案 1 :(得分:1)

现在可以使用新的答案,但使用Typed / Racket的替代方法:turnstile Racket库允许创建类型化语言,其中可以在编译时访问与表达式关联的类型。

不幸的是,我不清楚turnstile(还有?)提供一个详细的例子。