Search code examples
racketlisp-macros

How can I see expanded macros in racket?


I got this answer https://stackoverflow.com/a/70318991 about writing a simple macro that records the time at macro expansion time, and then always returns that time.

#lang racket

(begin-for-syntax
  (define the-time (current-seconds)))

(define-syntax (macro-expansion-seconds stx)
  (datum->syntax stx the-time))

(macro-expansion-seconds)
(macro-expansion-seconds)
(macro-expansion-seconds)

It works great, but now is there an easy way to see an expanded version of (macro-expansion-seconds) without evaluating it? (for debugging more complicated ones)


Solution

  • You can use

    (expand #'(macro-expansion-seconds))
    

    in the DrRacket repl.

    It will show you the graphical representation of a syntax object - remember to click the little arrow! In Mythical Macros I have written a little syntax objects.

    https://soegaard.github.io/mythical-macros/

    An alternative is to use the "Macro Stepper". Click the button in upper right corner of DrRacket: the icon is a combination of consists of a # and a play symbol.