2
votes

How can I access the types of a structure's members in the typed/racket language? The function extract-struct-info does give a list of field accessors but not their types.

(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)

Outputs:

(.#<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)'()

More generally, how can I access typed/racket's types, like getting the list of types in a union type (U Number String 'foo 'bar), or seeing the arguments to a polymorphic type?

I am interested only in macro-expansion-time access, not run-time access.

Related: Get the type information in macros (same question for Common Lisp)

2

2 Answers

3
votes

From good authority (the developers of Typed Racket) [from the #racket irc channel on freenode]:

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
votes

A new answer is now possible, but using an alternative to Typed/Racket: the turnstile Racket library allows creating typed languages in which the type associated with an expression can be accessed at compile-time.

Unfortunately, I do not know turnstile well enough (yet?) to provide a detailed example.