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

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

> net/uri-codec is not required (directly) into use.rkt. Instead, it is
> required at the definition of the macro and then a reference to that
> module is inserted into use.rkt. The macro system can track such
> references without explicitly having requires.

OK I see. Thanks!

> BTW, the same thing is happening with 'define'...  :)

Ah. Point.

(Although ... I was assuming `define' was imported on account of
use.rkt requiring racket or racket/base. So I wasn't surprised that
was working.)

On Sat, Oct 20, 2012 at 10:40 AM, Robby Findler
<robby at eecs.northwestern.edu> wrote:
> net/uri-codec is not required (directly) into use.rkt. Instead, it is
> required at the definition of the macro and then a reference to that
> module is inserted into use.rkt. The macro system can track such
> references without explicitly having requires.
>
> BTW, the same thing is happening with 'define'...  :)
>
> Robby
>
> On Sat, Oct 20, 2012 at 9:30 AM, Greg Hendershott
> <greghendershott at gmail.com> wrote:
>> 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"])))))
>>> ;;;;;;;;;;;;;;;;;;;;;;;;
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users

Posted on the users mailing list.