Racket: Using a macro across modules
In order to teach myself more advanced macros in racket, I set about creating a macro to increment a field in a mutable struct:
(increment! instance name field)
=>
(set-name-field instance (get-name-field instance))
I produced a macro that works, and decided it would be useful to share between multiple modules. Unfortunately, since the structure mutators are not in the scope of the module defining the macro, an expansion error occurs.
The following is a contrived example demonstrating the problem. I would like to know:
Did I write the macro code in idiomatic-racket style? Is this the correct approach?
How can I control the expansion of the macro, so that it operates in the presence of identifiers not found in it's original context?
Thanks.
#lang racket/load
(module util racket
(define-syntax increment!
(lambda (stx)
(syntax-case stx ()
开发者_Python百科 [(increment! s sn fn i)
(with-syntax
([set! (string->symbol
(format "set-~a-~a!" (syntax-e #'sn) (syntax-e #'fn)))]
[get (string->symbol
(format "~a-~a" (syntax-e #'sn) (syntax-e #'fn)))])
#'(set! s (+ i (get s))))]
;; default increment of 1
[(increment! s sn fn) #'(increment! s sn fn 1)])))
(provide increment!)
)
(module bank racket
(require 'util)
(struct money (dollars pounds euros) #:mutable #:transparent)
(let ([m (money 0 50 20)])
(increment! m money pounds 100)
(increment! m money dollars)
m)
)
(require 'bank)
Results in
expand: unbound identifier in module in: set-money-pounds!
You can't do just that. The problem is that you're generating symbols with the correct names, but you just return the symbols as-is, which means that with-syntax
gives them some default (and wrong) lexical context. Instead, you should use datum->syntax
and give it the correct context.
Below is a revision of your code that works as you expect. To see more about it, see my recent blog post on the subject of unhygienic macros.
However, this is not a robust solution. What happens when the setters and getters have different names? A more robust solution would be to use the struct name and extract the right information from it (at syntax time, in the macro) -- see the manual for details on this. It's also good to ask questions about it on the mailing list, since there might be a better way to get what you want, or a better solution if you're looking for some dot-notation-like functionality.
#lang racket/load
(module util racket
(define-syntax increment!
(lambda (stx)
(syntax-case stx ()
[(increment! s sn fn i)
(let ([id (lambda (fmt)
(let ([str (format fmt (syntax-e #'sn) (syntax-e #'fn))])
(datum->syntax #'sn (string->symbol str))))])
(with-syntax ([set! (id "set-~a-~a!")]
[get (id "~a-~a")])
#'(set! s (+ i (get s)))))]
;; default increment of 1
[(increment! s sn fn) #'(increment! s sn fn 1)])))
(provide increment!))
(module bank racket
(require 'util)
(struct money (dollars pounds euros) #:mutable #:transparent)
(let ([m (money 0 50 20)])
(increment! m money pounds 100)
(increment! m money dollars)
m))
(require 'bank)
精彩评论