Amazed by macros working on first attempt
- From: "jonathon" <j_mckitrick@xxxxxxxxxxx>
- Date: 28 Jul 2005 07:05:51 -0700
Admittedly, macros so far have been 'with-' style macros that don't
take a lot of thought. But I just ran into a situation that led me to
try them in a new way, and I was amazed.
I wanted one package to have access to values from another, but I
didn't want to export/expose the variables. So, I decided to add
accessor functions and create a new 'interface.lisp' module.
I started out with this type of function:
(defun get-account-fwd (account-name)
(cond
((equalp account-name (name *account-1*))
(balance-fwd *account-1*))
((equalp account-name (name *account-2*))
(balance-fwd *account-2*))))
Then I realized I could try this:
(defmacro defaccgetter (fname sname)
`(defun ,fname (aname)
(cond
((equalp aname (name *account-1*))
(,sname *account-1*))
((equalp aname (name *account-2*))
(,sname *account-2*)))))
(defaccgetter get-account-fwd balancefwd)
and a few others like this one. I worked on the syntax to get it right
before I tried building and just confusing myself, and when I was
satisfied, I added all of these 'getters' to the package export list.
I was *stunned* when it compiled and ran *flawlessly*. No problem with
sequence of macro expansion, export of generated function names, or
anything. It was great.
One improvement I would like to make would be to use some kind of
iterating or looping form to go through all the slots of a specific
object and apply that macro to them and then export the result, so I
can add a new slot and have the cross-package-accessor function
automatically generated and even exported.
How might I approach this problem?
.
- Follow-Ups:
- Re: Amazed by macros working on first attempt
- From: Thomas A. Russ
- Re: Amazed by macros working on first attempt
- From: Eric Lavigne
- Re: Amazed by macros working on first attempt
- Prev by Date: Re: delete-file & probe-file on directories
- Next by Date: Re: OT: Re: 8051 assembler
- Previous by thread: UFFI and calling Lisp functions from foreign code
- Next by thread: Re: Amazed by macros working on first attempt
- Index(es):
Relevant Pages
|