Find a module's name in racket?

Viewed 79

How does one acquire the name of the current module in a Racket?

Trying to write a macro that requires the current module name.

1 Answers

The current module name is not directly available to macros, because a module only really gets its name when the declaration is evaluated, not when the module is expanded. So you should not rely on knowing the module's name at compile time.

Instead, a macro can expand into code that gets the module's name when the module is run, using #%variable-reference and variable-reference->module-source. For example:

(define-syntax-rule (whereami)
  (variable-reference->module-source (#%variable-reference)))

The #%variable-reference form is a special bit of reflection that evaluates to a value containing information about the enclosing module. By "enclosing", I mean where the #%variable-reference expression occurs in the expanded code, not where the macro was defined.

Related