我有两个EIEIO课程:
(defclass i-driver ()
(;; more slots
(exit-conditions
:initarg :exit-conditions
:initform nil
:type list
:documentation
"Conditions to test in the main (while ...) expression"))
:documentation "This class describes a single driver of `i-iterate' macro")
和
(defclass i-spec ()
((exit-conditions
:type list
:reader i--get-exit-conditions
:documentation
"Conditions to test in the main (while ...) expression")
;; more slots
(drivers
:initform nil
:type list
:documentation
"This slot contains the list of all drivers used in this iteration macro"))
:documentation "This class contains a specification of the
expansion of the `i-iterate' macro")
我想做什么:
exit-conditions
类展开i-spec
字段,将其从i-driver
个对象列表中聚合。我最初的想法是我可以定义一个读者,如:(defmethod i--get-exit-conditions ((spec i-spec))
(with-slots ((ds drivers)) spec
(let (result)
(while ds
(push (oref ds exit-conditions) result)
(setq ds (cdr ds)))
result)))
exit-conditions
中分配广告位i-spec
,因为它只需要存储在i-driver
中。 PS。如果是版权声明,名称中的i
适用于iterate
,则不适用于Wozniak在Apple产品中使用的任何内容:)
编辑:
以下是我现在的做法:
(defmethod i-aggregate-property ((spec i-spec) property &optional extractor)
(with-slots (drivers) spec
(let ((ds drivers)result)
(while ds
(if extractor
(setq result
(funcall extractor (slot-value (car ds) property) result))
(push (slot-value (car ds) property) result))
(setq ds (cdr ds))) result)))
这是丑陋的样子:
(defmacro i-iterate (&rest specs)
(let ((spec (i--parse-specs specs)))
(with-slots (body result) spec
(let* ((exit-conditions
(i-aggregate-property spec 'exit-conditions #'append))
(catch-conditions
(i-aggregate-property spec 'catch-conditions #'append))
(variables
(i-aggregate-property spec 'variables #'append))
(actions
(i-aggregate-property spec 'actions #'append))
(econds
(cond
((cdr exit-conditions)
(append '(and) (nreverse exit-conditions)))
(exit-conditions (car exit-conditions))
(t t)))
(vars (nreverse variables))
(body (append actions (nreverse body))))
(cond
((and catch-conditions vars)
(append catch-conditions
(list
`(let* (,@vars)
(while ,econds ,@body) result))))
(catch-conditions
(append catch-conditions
(list
`(while ,econds ,@body) result)))
(variables
`(let* (,@vars)
(while ,econds ,@body) ,result))
(t `(progn (while ,econds ,@body) ,result)))))))
我可以添加一个宏来隐藏这些重复的调用,并且有类似with-slots
的内容,但如果我不需要,我会更开心。
答案 0 :(得分:0)
为什么不直接在exit-conditions
而不是插槽中指定通用函数i-spec
?