views:

109

answers:

2

I have about two macros (and climbing) in my codebase that look like this:

(defmacro def-stat-method (method-name stat)
  `(progn
     (defmethod ,method-name ((monster monster))
       (getf (stats monster) ,stat))
     (defmethod (setf ,method-name) (value (monster monster))
       (setf (getf (stats monster) ,stat) value))))

and this:

(defmacro def-energy-method (method-name stat)
  `(progn
     (defmethod ,method-name ((monster monster))
       (getf (energies monster) ,stat))
     (defmethod (setf ,method-name) (value (monster monster))
       (setf (getf (energies monster) ,stat) value))))

Each of the macros has the following calling syntax: (def-stat-method ranged-weapon :ranged-weapon)

I would like a macro (def-foo-method macro-name method) that expands into the appropriate macros so that (def-foo-method def-stat-method stats) expands into the first example above. I am a lisp newbie and don't really know how to do this. All help appreciated.

+6  A: 

Just write a macro that expands into another defmacro, like this:

(defmacro def-foo-method (macro-name method)
  `(defmacro ,macro-name (method-name stat)
     (let ((method ',method))
       `(progn
          (defmethod ,method-name ((monster monster))
            (getf (,method monster) ,stat))
          (defmethod (setf ,method-name) (value (monster monster))
            (setf (getf (,method monster) ,stat) value))))))

I think there might be a way to do this without the let, but nested backticks are confusing :)

wdebeaum
Thank you. I'm working on a GPL roguelike game called Menace of the Mines (http://motm.sourceforge.net/) and just want to make sure putting that in there is OK with you. (I have an attributing comment)
krzysz00
That's fine with me (sorry for the delayed reply).
wdebeaum
+1  A: 

To me that code looks slow. An object-oriented runtime dispatch plus a search on a property list? Why?

Sometimes property lists are added to CLOS objects to provide storage for not often used slots or to make them easily extensible (where all kinds of properties could be added without knowing them all before running the program). Generally it might be better to make the properties real slots of the object.

Rainer Joswig
you're saying the whole plist thing should be scrapped and I should get even more slots.Sounds like an OK idea
krzysz00
Making it a normal accessor would be the default idea. If you use a plist, you should ask yourself, why? If you can answer that question with confidence, then go ahead.Usually when you code in Lisp, if you have time for a design iteration, you should look at your code and get rid of the fat. Sometimes Lisp has very elegant mechanisms and if not, it might help to introduce an elegant mechanism, so that the application code can be simplified.
Rainer Joswig