[racket] A macro's `require' works in module but not in REPL

From: Greg Hendershott (greghendershott at gmail.com)
Date: Sat Oct 20 10:30:26 EDT 2012

1. Thank you!
2. This works. Hooray.
3. I don't understand why it works. Boo.

My reasoning:

In def.rkt, `define-foo' generates syntax. The syntax is a procedure
definition. The body of the defined procedure needs to use a function
from another module (in this case net/uri-codec).

When Racket is compiling use.rkt, it substitutes `(define-foo)' with
the syntax from def.rkt. However, if that new syntax doesn't include a
`(require net/uri-codec)', how the heck does net/uri-codec get
required into use.rkt?

On Fri, Oct 19, 2012 at 7:15 PM, Danny Yoo <dyoo at hashcollision.org> wrote:
> On Fri, Oct 19, 2012 at 9:04 AM, Greg Hendershott
> <greghendershott at gmail.com> wrote:
>> I need to fix a bug, https://github.com/greghendershott/gapi/issues/2 .
>>
>> I've distilled to a simple case that exhibits the behavior:
>>
>> ;; def.rkt
>> #lang racket
>> (provide define-foo)
>> (define-syntax define-foo
>>   (lambda (stx)
>>     #`(begin
>>         (require net/uri-codec)
>>         (define (#,(datum->syntax stx 'foo))
>>           (alist->form-urlencoded '([a . "1"]))))))
>
>
>
> The macro's outputted code appears to require net/uri-codec.  But
> rather that require be part of the macro's output, you probably should
> just modify def.rkt so that it requires net/uri-codec directly.
>
> Basically, change def.rkt and lift the require up:
>
> ;;;;;;;;;;;;;;;;;;;;;;;;
> #lang racket
> (provide define-foo)
>
> (require net/uri-codec)
> (define-syntax define-foo
>   (lambda (stx)
>     #`(define (#,(datum->syntax stx 'foo))
>         (alist->form-urlencoded '([a . "1"])))))
> ;;;;;;;;;;;;;;;;;;;;;;;;

Posted on the users mailing list.