<div class="h5"><br>
</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I meant in the argument to `datum->syntax'...<br></blockquote></div><br>Ah, great, this is even better.<br>
I can't believe its so small in the end...<br><br>For the sake of it, here is what we have now:<br><br>#lang racket<br><br>(provide define-syntax-rule/id)<br><br>;;; By Jon Rafkind (+ tweakings)<br><br>;; Like define-syntax-rule, but takes an additional ([id id-rename] ...) set<br>
;; where the id can be used in body, <br>;; and each id in body is replaced by the result of the id-rename expression.<br>(define-syntax (define-syntax-rule/id stx)<br> (syntax-case stx ()<br> [(_ (name arg ...)<br> ([id id-rename] ...)<br>
body ...)<br> #'(define-syntax (name stx2)<br> (syntax-case stx2 ()<br> [(_ arg ...) ; This comes from above!<br> ; We want the defined ids to have the same lexical scope as<br>
; the defined macro itself.<br> (with-syntax ([id (datum->syntax<br> stx2<br> (string->symbol<br> ; id-rename is an expression that comes from above.<br>
; it will be replaced by its code in the first expansion<br> ; (the first syntax-case),<br> ; and this code will be executed in the second expansion.<br>
(let ([arg (syntax-e #'arg)] ...)<br> id-rename)) <br> stx2 stx2 stx2<br> )]<br> ...)<br>
; id is correctly bound in body here because<br> ; body is replaced by the full expression (containing id)<br> ; in this submacro (just like for id-rename).<br> #'(begin body ...))]))<br>
]))<br><br>#| TESTS: |#<br><br>; use the macro stepper to see how it is expanded<br>(define-syntax-rule/id (make-getter-setter xid1 xid2)<br> ([getter (format "get-~a-~a" xid1 xid2)]<br> [setter (format "set-~a-~a" xid1 xid2)]<br>
[xchger (format "xch-~a-~a" xid1 xid2)])<br> (define (getter) (values xid1 xid2))<br> (define (setter v1 v2) (set! xid1 v1) (set! xid2 v2))<br> (define (xchger) (setter xid2 xid1))<br> )<br><br>(define x1 5)<br>
(define x2 6)<br>(make-getter-setter x1 x2)<br>(get-x1-x2)<br>(set-x1-x2 10 11)<br>(get-x1-x2)<br>(xch-x1-x2)<br>(get-x1-x2)<br><br>;|#<br>