How do I write a macro-defining macro in common lisp

百般思念 提交于 2019-12-05 01:26:05

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 :)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!