EIEIO组合(通过自己的插槽暴露包含对象的插槽)

时间:2012-11-21 13:57:00

标签: oop emacs lisp elisp composition

我有两个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的内容,但如果我不需要,我会更开心。

1 个答案:

答案 0 :(得分:0)

为什么不直接在exit-conditions而不是插槽中指定通用函数i-spec