CLOS:使用一个槽值来初始化另一个槽

CLOS: Use a slot value to initialize another slot

我对使用 CLOS 还很陌生。在这里,我写了一种使用 CLOS 定义队列的可能方法:

(defclass Queue ()
    ((queue-size
        :reader queue-size
        :initarg :queue-size
        :initform (error "Provide a queue-size when initialising a Queue")
        :type number)
    (elements
        :accessor elements
        :initform (make-array queue-size :initial-element nil))
    (put-ptr
        :accessor put-ptr
        :initform 0
        :type number)
    (get-ptr
        :accessor get-ptr
        :initform 0
        :type number)))

如您所见,我使用槽 queue-size 的值在槽 elements 中创建数组。但是,不幸的是,这给了我以下错误:

*** - DEFAULT-ELEMENTS: variable QUEUE-SIZE has no value

正如我所说,我是 CLOS 的新手。有什么办法我仍然可以这样做吗?是否可以覆盖某种初始化方法?如果是,我该怎么做?

在 CLOS 中,您不能直接将槽作为变量引用。此外,您不能在 initforms.

中引用对象的其他插槽

例子,简化:

CL-USER 27 > (defclass queue () (size elements))
#<STANDARD-CLASS QUEUE 4020001AB3>

CL-USER 28 > (describe (make-instance 'queue))

#<QUEUE 40200040CB> is a QUEUE
SIZE          #<unbound slot>
ELEMENTS      #<unbound slot>

我们现在设置 elements 插槽:

CL-USER 36 > (defclass queue () ((size :initarg :size) elements))
#<STANDARD-CLASS QUEUE 42E0A2DBFB>

为此,我们编写了一个 initialize-instance :after 方法。 通常的初始化发生在 之后 我们的方法运行。 WITH-SLOTS 允许我们在代码中使用某些槽,例如变量。在这里我们访问插槽 sizeelements:

CL-USER 37 > (defmethod initialize-instance :after ((q queue) &rest initargs)
               (with-slots (size elements) q
                 (setf elements (make-array size :initial-element nil))))
#<STANDARD-METHOD INITIALIZE-INSTANCE (:AFTER) (QUEUE) 402000ADD3>

没有 WITH-SLOTS,使用函数 SLOT-VALUE,它看起来像这样:

CL-USER 38 > (defmethod initialize-instance :after ((q queue) &rest initargs)
               (setf (slot-value q 'elements)
                     (make-array (slot-value q 'size) :initial-element nil)))

示例:

CL-USER 39 > (describe (make-instance 'queue :size 10))

#<QUEUE 402000BE0B> is a QUEUE
SIZE          10
ELEMENTS      #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL)