[plt-scheme] adding syntax transfomer to a namespace

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Fri Dec 14 11:20:43 EST 2007

At Fri, 14 Dec 2007 17:10:30 +0100, "Jos Koot" wrote:
> (define (add-binding name transformer)
>  (eval #`(define-syntax #,name #,transformer) namespace))
> 
> (add-binding 'name (syntax-rules () ((_) (list 'transformer))))
> [...]
> 
> (define (add-binding name transformer)
>  (eval #`(define-syntax #,name #,transformer) namespace))
> 
> (add-binding #'name (syntax-rules () ((_) (list 'transformer))))
>              ^^^^^^

Yes, these are different in the specific case of binding 'name, since a
symbol to `add-binding' is coerced to a syntax object using the lexical
context of the `quasisyntax' expression --- and that context includes a
local binding of `name'.


I should have suggested

 (define (add-binding name transformer)
   (with-syntax ([name (datum->syntax #f name)])
     (eval #`(define-syntax name #,transformer))))

to avoid that particular pitfall.


Matthew



Posted on the users mailing list.