Hard-coded 'require' from a racket macro doesn't bind 'provided' identifiers

25 views Asked by At

I'm struggling to understand what is conceptually different between these two simple macros that is causing the import-bad version to fail with 'unbound identifier' errors.

imports-mod.rkt exports myid:

#lang racket/base
(provide myid)
(define (myid x)x)
(displayln "imported mod")

imports.rkt defines the macros that require "imports-mod.rkt", and tries to use import-bad:

#lang racket/base

(require (for-syntax racket/base syntax/parse racket/syntax))

(define-syntax (import-good stx)
  (syntax-parse stx
    [(_ mod-id:string)
     (define s (syntax/loc stx (require mod-id)))
     (displayln s)
     s]
    [else #'(displayln "no")]))


(define-syntax (import-bad stx)
  #'(require "imports-mod.rkt"))


(import-bad "imports-mod.rkt")
(myid 123)
;; ^^^ This results in ` myid: unbound identifier in: myid`

It works if import-good is used instead.

Examining the expansion through the macro stepper for import-good and import-bad shows that they appear to expand to the same code: (require "imports-mod.rkt")

But something must be different about them, and I suspect this is to do with the mod-id template variable having some special property that I'm missing.

If I just use import-bad, without trying to call myid, I see the output imported mod which comes from imports-mod.rkt. So it is actually loading the module, just not binding the myid identifier.

0

There are 0 answers