根据 Common Lisp 对象系统类定义中的其他槽值初始化槽

发布于 2024-09-16 17:42:30 字数 470 浏览 15 评论 0原文

在我的类定义中,我想根据另一个槽的值初始化一个槽。这是我想做的事情:

(defclass my-class ()
  ((slot-1 :accessor my-class-slot-1 :initarg slot-1)
   (slot-2 :accessor my-class-slot-2 :initform (list slot-1))))

但是这不能编译:

1 compiler notes:

Unknown location:
  warning: 
    This variable is undefined:
      SLOT-1

  warning: 
    undefined variable: SLOT-1
    ==>
      (CONS UC-2::SLOT-1 NIL)


Compilation failed.

有办法做到这一点吗?

In my class definition, I want to initialize one slot based on the value of another slot. Here is the sort of thing I would like to do:

(defclass my-class ()
  ((slot-1 :accessor my-class-slot-1 :initarg slot-1)
   (slot-2 :accessor my-class-slot-2 :initform (list slot-1))))

However this doesn't compile:

1 compiler notes:

Unknown location:
  warning: 
    This variable is undefined:
      SLOT-1

  warning: 
    undefined variable: SLOT-1
    ==>
      (CONS UC-2::SLOT-1 NIL)


Compilation failed.

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁许谁一生繁华 2024-09-23 17:42:30

Use initialize-instance :after documented here

数理化全能战士 2024-09-23 17:42:30

这是 Doug Currie 的答案扩展:

(defclass my-class ()
  ((slot-1 :accessor my-class-slot-1 :initarg :slot-1)
   (slot-2 :accessor my-class-slot-2)))

(defmethod initialize-instance :after 
           ((c my-class) &rest args)
  (setf (my-class-slot-2 c) 
        (list (my-class-slot-1 c))))

这是一个显示它有效的调用:

> (my-class-slot-2 (make-instance 'my-class :slot-1 "Bob"))
("Bob")

请参阅 这篇文章了解更多详情。

Here is Doug Currie's answer expanded:

(defclass my-class ()
  ((slot-1 :accessor my-class-slot-1 :initarg :slot-1)
   (slot-2 :accessor my-class-slot-2)))

(defmethod initialize-instance :after 
           ((c my-class) &rest args)
  (setf (my-class-slot-2 c) 
        (list (my-class-slot-1 c))))

Here's a call showing that it works:

> (my-class-slot-2 (make-instance 'my-class :slot-1 "Bob"))
("Bob")

See this article for more details.

够运 2024-09-23 17:42:30
(defparameter *self-ref* nil)


(defclass self-ref ()
  ()

  (:documentation "
Note that *SELF-REF* is not visible to code in :DEFAULT-INITARGS."))


(defmethod initialize-instance :around ((self-ref self-ref) &key)
  (let ((*self-ref* self-ref))
    (when (next-method-p)
      (call-next-method))))



(defclass my-class (self-ref)
  ((slot-1 :accessor slot-1-of :initarg :slot-1)
   (slot-2 :accessor slot-2-of
           :initform (slot-1-of *self-ref*))))




CL-USER> (let ((it (make-instance 'my-class :slot-1 42)))
           (values (slot-1-of it)
                   (slot-2-of it)))
42
42
CL-USER> 
(defparameter *self-ref* nil)


(defclass self-ref ()
  ()

  (:documentation "
Note that *SELF-REF* is not visible to code in :DEFAULT-INITARGS."))


(defmethod initialize-instance :around ((self-ref self-ref) &key)
  (let ((*self-ref* self-ref))
    (when (next-method-p)
      (call-next-method))))



(defclass my-class (self-ref)
  ((slot-1 :accessor slot-1-of :initarg :slot-1)
   (slot-2 :accessor slot-2-of
           :initform (slot-1-of *self-ref*))))




CL-USER> (let ((it (make-instance 'my-class :slot-1 42)))
           (values (slot-1-of it)
                   (slot-2-of it)))
42
42
CL-USER> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文