0

我有两个 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-speci-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-conditionsi-spec因为它只需要存储在i-driver.
  • 我也希望槽是只读的(只能通过修改相应的驱动来修改,不能通过写入槽本身来修改)。

PS。在版权声明的情况下,i在名称中是为了iterate,而不是为了沃兹尼亚克在苹果产品中使用的任何东西:)

编辑:

这是我现在的做法:

(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,但如果我不需要的话,我会更开心。

4

1 回答 1

0

你为什么不只定义一个exit-conditions指定的通用函数i-spec而不是一个插槽?

于 2012-11-21T16:04:19.987 回答