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



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

(需要(语法球拍/结构信息                     语法/解析))(定义语法(显示结构信息STX)  (语法解析 stx    [(显示结构信息名称:id)     (显示(提取结构信息(语法本地值#'name)))     #'(列表)]))(结构: s ([a : 数字] [b : 字符串]))(显示结构信息 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)'()

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

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

相关:获取宏中的类型信息(Common Lisp 的问题相同)

来自良好的权威(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

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

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

最新更新