It Seemed Like a Good Idea at the Time Coding, Mostly

21Mar/092

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.

(define-syntax convenient-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.

(define (much-harder-function arg1 arg2)
  (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.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Comments (2) Trackbacks (0)
  1. Why wasn’t the function in scope of the macro?

  2. 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.


Leave a comment


No trackbacks yet.