Kevin,

I have made this exact mistake in the past. The trouble is with the
lexical context being passed to `format-id`.

(_foo 3)
foo3
;; 3

Here, _foo is passed the syntax #'(_foo 3), which came from the same
environment as the reference, foo3.

(foo 3)
foo3
;; error ...

Here, _foo is passed the syntax #'(_foo 3), which was created *by the
foo macro*, in a different context to the foo3 reference.

The solution is to pass #'n as the first argument to format-id.
Hopefully this explanation made some sense.

- Sam

On Fri, May 20, 2016 at 5:36 PM, Kevin Forchione <lyss...@gmail.com> wrote:

> Hi guys,
> I’ve been interested in having a macro build a series of defines. So I
> decided to start small, trying to get to a macro that would do something
> like the following to begin with:
>
> >(foo 3)
> (define foo1 1)
> (define foo2 2)
> (define foo3 3)
>
> I start with a macro that appears to do a single define:
>
> (require (for-syntax racket/syntax))
>
> (define-syntax (_foo stx)
>   (syntax-case stx ()
>     [(_ n) (with-syntax ([id (format-id stx "foo~a" (syntax-e #'n))])
>              #'(define id n))]))
>
>
> And this appears to work for (_foo 3) for instance.
>
> Then I create a macro that calls this macro:
>
> (define-syntax (foo stx)
>   (syntax-case stx ()
>     [(foo n0) #'(_foo n0)]
>     [(foo n0 n ...) #'(begin
>                       (_foo n0)
>                       (foo n ...))]))
>
> thinking that I could do something like (foo 1 2 3) as an intermediary
> step.  So I test it with (foo 3) for instance, expecting it to define foo3
> and the macro debugger tells me that it’s created (define foo3 3), but that
> foo3 is bound as foo3.0, which is a mystery to me as I thought building the
> id using with-syntax and format-id  would have sorted the binding for this.
>
> Looks like I’m at a learning moment…. any explanation why executing (_foo
> 3) at the real works and (foo 3) does not?
>
> Thanks!
> -Kevin
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to