我试图在Common Lisp中编写一个宏来定义一个带有我指定的变量槽的类。到目前为止,它的工作正常(而且我对clisp印象非常深刻!):
(defmacro notifier (class slot)
"Defines a setf method in (class) for (slot) which calls the object's changed method."
`(defmethod (setf ,slot) (val (item ,class))
(setf (slot-value item ',slot) val)
(changed item ',slot)))
(defmacro notifiers (class slots)
"Defines setf methods in (class) for all of (slots) which call the object's changed method."
`(progn
,@(loop for s in slots collecting `(notifier ,class ,s))))
(defmacro defclass-notifier-slots (class nslots slots)
"Defines a class with (nslots) giving a list of slots created with notifiers, and (slots) giving a list of slots created with regular accessors."
`(progn
(defclass ,class ()
( ,@(loop for s in nslots collecting `(,s :reader ,s))
,@(loop for s in slots collecting `(,s :accessor ,s))))
(notifiers ,class ,nslots)))
问题是,现在我想创建的不是只是我在宏调用中指定的插槽,而是一些其他插槽以及变体名称。为了做到这一点,我必须使用一个丑陋的"符号名称,改变字符串,实习生"生成变种名称作为插槽名称的序列,我已经看过SO的答案,说你应该避免这样做。那么有更好的方法吗?