Fixing Broken Macros with Eval and Quasiquote
Recently, I found myself needing to deal with a "convenience" macro, which quoted several of its arguments for me before passing them along to the real function. Unfortunately, only the macro was exported from the library, and I was unable to access the base function.
(syntax-rules ()
((_ arg1 arg2) (much-harder-function 'arg1 'arg2))))
How useful. To save me a handful of quotes, I lose the ability to programmatically generate my arguments.
As I was loath to reimplement the entire library just to regain that ability, I looked for another solution. Thankfully, I found one.
(eval `(convenient-function ,arg1 ,arg2)
(environment '(convenient-lib))))
As far as I can tell, this circumvents the macro's quoting features entirely to allow me to pass in a dynamically generated symbol. While it won't work with functions which modify the environment, it worked well in my case, and allowed me to move on to more interesting code.













March 22nd, 2009 - 18:23
Why wasn’t the function in scope of the macro?
March 23rd, 2009 - 14:50
The function wasn’t visible because I was dealing with an R6RS library which only exported the macro. I couldn’t just modify the library because it was a standard library for the implementation, and I would prefer not to modify those if I can help it, as it hinders upgrades quite a bit.