覆盖子class中class插槽的:INITFORM

Override :INITFORM of the class slot in subclass

我需要稍微概括一下子类中的默认槽值。

示例:

(defclass class-a ()
  ((slot-1 :initarg :slot-1 :initform #'identity)
   <...> other-slots<...>))

它的子类是

(defclass class-b (class-a)
  ((slot-2 :initarg :slot-2 :initform 0)))

但是#'IDENTITY作为默认值还是不够好,稍微笼统一些

(lambda (&rest x) x)

会更适合,因为它需要多个参数(我认为这与 Liskov 原则不矛盾)。为 CLASS-B 覆盖 :INITFORM 的最佳方法是什么?

  1. 我可以为 CLASS-B 添加 INITIALIZE-INSTANCE :AFTER 并查看 SLOT-1 是否设置为 #'IDENTITY 并覆盖它。
  2. 如果我在 CLASS-B 中重新引入 SLOT-1 会怎样?我想避免它,因为我必须为它重复所有插槽信息。

What happens if I reintroduce SLOT-1 in CLASS-B?

有效。

I would want to avoid it as I would have to repeat all the slot information for it.

没有。只有区别。

一个简单的替代方法是使用 :default-initargs 作为 class 而不是 :initform 作为插槽。在那种情况下,您只需要为 subclass 中的那个插槽提供一个新的默认初始化参数。有关示例,请参阅 Chris Riesbeck's rationale for :default-initargs and see this lisptip