[plt-scheme] Recognising identifiers bound with a certain macro

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Fri Feb 8 11:54:06 EST 2008

Thanks! I've got it working and I understand what's going on.

-- Dave

> Oh, right. You need to explicitly bind the identifier `a' to a macro
> that has whatever compile-time information you need in the sql macro.
> In this case, maybe all you need is the name of the identifier that
> has the real binding? If so, your code would look something like this:
>
> #lang scheme
>
> (define-syntax (def stx)
>  (syntax-case stx ()
>    [(_ id exp)
>     #'(begin
>         (define secret-binding exp)
>         (define-syntax id #'secret-binding))]))
>
> (define-syntax (use stx)
>  (syntax-case stx ()
>    [(_ id)
>     (syntax-local-value #'id)]))
>
> (def a 1)
> (use a)
>
> To do a better job with error messages you would want to bind `a' to a
> transformer that signals a syntax error. There are a number of ways to
> do this. Here's one (you can also bind it to a struct procedure if
> that works better):
>
> #lang scheme
>
> (define-syntax (def stx)
>  (syntax-case stx ()
>    [(_ id exp)
>     #'(begin
>         (define secret-binding exp)
>         (define-syntax id
>           (case-lambda
>             [(stx) (raise-syntax-error #f "used variable out of  
> context" stx)]
>             [() #'secret-binding])))]))
>
> (define-syntax (use stx)
>  (syntax-case stx ()
>    [(_ id)
>     ((syntax-local-value #'id))]))
>
> (def a 1)
> (use a)
> a
>
> hth,
> Robby


Posted on the users mailing list.