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 ()
[(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!