Lisp: How can i get hold of the created instance inside initialize-instance :around method

StackOverflow https://stackoverflow.com/questions/5817100

  •  26-10-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top