【发布时间】:2012-03-14 23:36:47
【问题描述】:
我有以下课程,而且更喜欢他们:
(defclass weapon ()
((base-slice-damage
:documentation "Base slice damage dealt by weapon"
:reader base-slice-damage
:initform 0
:initarg :base-slice-damage)
(base-blunt-damage
:reader base-blunt-damage
:initform 0
:initarg :base-blunt-damage)
(base-pierce-damage
:reader base-pierce-damage
:initform 0
:initarg :base-pierce-damage)))
(defclass dagger (weapon)
((base-slice-damage
:initform 3)
(base-pierce-damage
:initform 6)))
(defclass attack ()
((slice-damage-dealt
:initarg :slice-damage-dealt
:reader slice-damage-dealt)
(blunt-damage-dealt
:initarg :blunt-damage-dealt
:reader blunt-damage-dealt)
(pierce-damage-dealth
:initarg :pierce-damage-dealt
:reader pierce-damage-dealt)))
如您所见,有很多重复。对于其中两个类,我的插槽都具有相同的选项,并且仅根据它们是切片、钝还是穿孔而有所不同。
我考虑过使用宏来定义属性类,然后将它们混合在一起。这就是我目前所拥有的:
(defmacro defattrclass (attr-name &body class-options)
`(defclass ,(symb attr-name '-attr) ()
((,attr-name
,@class-options))))
但这还远远不够。
编辑:
我想出了这个,虽然我并不完全满意:
(defmacro defattrclass (attr-name &body class-options)
`(defclass ,(symb attr-name '-attr) ()
((,attr-name
,@class-options))))
(defmacro defattrclasses (attr-names &body class-options)
`(progn
,@(loop for attr-name in attr-names collect
`(defattrclass ,attr-name ,@class-options))))
【问题讨论】:
-
reddit.com/r/lisp/comments/qwy5o/…(如果您同时在两个地方提问,那么明确的网络礼仪是很好的。)
-
谢谢,我没有意识到
标签: macros lisp common-lisp