Question

In Common-Lisp CLOS

Is it possible to dynamically add one more super class in existing class.

Update:

I wanted to defined defassoc kind of macro that will associated some behaviour with method/function using same argument

e.g.

(defassoc (gname (s (g group)))
    ((name1 (name ((corresponding-task task g) s)))
     (record1 (record ((corresponding-task task g) s))))
  (let ((n name1)
        (r record1))
    (if (and name1 record1)
        (display name1 record1)
        (call-next-method))))

expand to

(symbol-macrolet ((name1 (name ((corresponding-task task g) s)))
                  (record1 (record ((corresponding-task task g) s))))
  (defmethod gname :after (s (g group))
            (let ((n name1) (r record1))
              (if (and name1 record1)
                  (display name1 record1)
                  (call-next-method)))))

Here it is ensuring when ever (gname (s (g group)) is call here should be call for corresponding task to group

(name ((corresponding-task task g) s)
(record ((corresponding-task task g) s)

I used this macro

(defmacro defassoc ((main-method main-method-lambda-list)
                    funspec-list &body body)
  `(symbol-macrolet ,(mapcar (lambda (fspec)
                               (destructuring-bind (name f) fspec
                                 (list name f)))
                             funspec-list)
     (defmethod
       ,main-method ,mod ,main-method-lambda-list
       ,@(if body
             body
             `(if (and
                   ,@(mapcar (lambda (e)
                               (car e))
                             funspec-list))
                  (call-next-method)))))

But problem is that it will overwrite the

(defmethod gname :after (s (g group))
      ...)

(If it has any, I can verify it if it has one or not,)

But I want it to work for any object whether it has that method to overwrite or not

So basically it should be needed to change in code for any thing in that classes and methods.

So one way I decided that to dynamically add parent class to define this method on it.

Other way could be defadvide or fwrapper but it is not present in SBCL.

Was it helpful?

Solution

Yes, it's possible. The easiest way would be to simply redefine the class. You do that by issuing another call to DEFCLASS. If you want to do more complicated things, you have to resort to the MOP (MetaObject Protocol). Essentially everything you'd ever want to do is possible using the MOP, but I would need more detailed information as to what it is you're trying to do in order to explain it further.

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