[racket] getting one macro to tell another macro to define something

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Fri Aug 1 17:39:14 EDT 2014

Use syntax-local-introduce when putting syntax into a side-channel or 
getting it back out across macro calls. This only matters when the 
syntax represents a definition or more generally contains binders whose 
references are not all in the same syntax.

Here are the two places that need changing:

(define-syntax sender
   (lambda (stx)
     (syntax-parse stx
       [(sender def)
        (define defs (syntax-parameter-value #'current-defs))
        (set-add! defs (syntax-local-introduce #'def))

(define-syntax reciever
   (lambda (stx)
     (syntax-parse stx
        (define defs (syntax-parameter-value #'current-defs))
        (with-syntax ([(def ...)
                       (map syntax-local-introduce (set->list defs))])
          #'(begin def ...))])))


On 08/01/2014 05:12 PM, Alexander D. Knauth wrote:
> Is there any way to do something like this and have it work?:
> #lang racket
> (require racket/stxparam
>           (for-syntax syntax/parse
>                       racket/set))
> ;; current-defs : (syntax-parameter-of (or/c set-mutable? #f))
> (define-syntax-parameter current-defs #f)
> (define-syntax sender
>    (lambda (stx)
>      (syntax-parse stx
>        [(sender def)
>         (define defs (syntax-parameter-value #'current-defs))
>         (set-add! defs #'def)
>         #'(void)])))
> (define-syntax reciever
>    (lambda (stx)
>      (syntax-parse stx
>        [(reciever)
>         (define defs (syntax-parameter-value #'current-defs))
>         (with-syntax ([(def ...) (set->list defs)])
>           #'(begin def ...))])))
> (syntax-parameterize ([current-defs (mutable-set)])
>    (sender (define x 1))
>    (reciever)
>    x)
> Right now, x is undefined.
> I’m guessing that that’s because it has the wrong syntax marks, but is there any way around that?
> (Other that doing (datum->syntax stx (syntax->datum …)) on everything?)
> ____________________
>    Racket Users list:
>    http://lists.racket-lang.org/users

Posted on the users mailing list.