Lisp:我如何获取在initialize-instance :around方法中创建的实例

发布于 2024-11-03 12:52:48 字数 672 浏览 5 评论 0原文

我想为类 X 创建一个初始化实例的 (:around Qualified) 专门化器,它将首先调用下一个方法,然后调用另一个类的 make-instance,为其提供创建的 X 实例。我怎样才能获取在initialize-instance :around 方法中创建的实例? (当然假设在调用了 call-next-method 之后,所以我们处于 :around 的完成端)

编辑: A 类与 B 类通过每个类中的一个槽保持另一个类的 id,具有双向关系,但 A 类需要 B 类的 id,而相反则不需要。所以我想要的流程是:

  1. make-instance 'classA
  2. inside initialize-instance :around classA 我会有:

    我。 make-instance classB 并获取 id-of-B。

    ii 调用下一个方法添加 id-of-B

    iii 将 classB 的相应 id-of-A 插槽设置为我们创建的 classA 实例(这是我最初的问题原因)

现在我可以在 :before 和 [ 中执行 [i] iii] 在:之后,但我不能: 类 A 和 B 是通过大象的持久类,我想将整个流程包装在一个事务中,我不想跨越许多方法。对于那些熟悉大象的人来说,我想使用确保事务包装器,并且我不想在不同的点使用显式的开始和提交函数调用。

I want to create an (:around qualified) specializer of initialize-instance for a class X that will first call-next-method and then will call make-instance of another class, supplying it with the created instance of X. How can i get hold of the created instance inside initialize-instance :around method? (assuming of course after having called call-next-method so we are at the finilizing side of :around)

EDIT:
Class A has a bi-directional relationship with Class B through a slot in each one that keeps the id of the other, but class A requires id of class B, while the opposite is not required. So the flow i want is:

  1. make-instance 'classA
  2. inside initialize-instance :around classA I would have:

    i. make-instance classB and aquire id-of-B.

    ii call-next-method adding id-of-B

    iii set the corresponding id-of-A slot of classB point to our created classA instance (this is my original question reason)

Now I could do [i] in :before and [iii] in :after, but i cant:
Classes A and B are persistent classes through elephant and I want to wrap the whole flow in a transaction which I wouldnt like to span many methods. For those familiar with elephant, I want to use ensure-transaction wrapper, and I dont want to use explicit begin and commit function calls in different points.

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

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

发布评论

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

评论(2

泪意 2024-11-10 12:52:48

该实例作为initialize-instance 的第一个参数传递。

(defmethod initialize-instance :around ((created myclass) ...)
  ;; do something with created
  created)

The instance is passed as the first argument of initialize-instance.

(defmethod initialize-instance :around ((created myclass) ...)
  ;; do something with created
  created)
冷︶言冷语的世界 2024-11-10 12:52:48

您通常会使用 :after 方法而不是 :around 方法来执行此操作。除非您深入研究 CLOS 的内部,否则对未初始化的实例执行任何操作都没有多大意义。该实例作为initialize-instance 的第一个参数提供。

如需更深入的处理,请参阅CLHS,第 7.1 节

You would usually do that with an :after method, not an :around method. Unless you fiddle around deeply in the guts of CLOS, it does not make much sense to do anything with an uninitialized instance. The instance is supplied as the first argument of initialize-instance.

For a deeper treatment, look at the CLHS, section 7.1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文