[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: add-slot

    Date: Fri, 28 Apr 89 10:05:53 PST
    From: goldman@vaxa.isi.edu

    Can someone give me a formula for using PCL's MOP to add a new slot
    to an existing class?

The following function does this.  The initfunction argument must be a
function of no arguments that will be called to initialize the slot.

Note that it doesn't have any error checking (for example to make sure
there isn't already a slot by that name.  You will probably want to make
some other changes to this function to get the precise interface you
want.  But, this example shows the basic functionality you need.

Let me know if you need any more help with this.

(defun add-slot (class name initform initfunction &rest other-slot-options)
  (let ((slotd (apply #'make-instance 'standard-slot-description
				      :name name
				      :allocation :instance
				      :initform initform
				      :initfunction initfunction
	(old-direct-slots (class-local-slots class)))
    (update-class class :direct-slots (cons slotd old-direct-slots))))

The following dialogue shows using the add-slot function.

(defclass foo ()
     ((x :initform 0)
      (y :initform 1)))
#<FOO 101060512>

(describe (make-instance 'foo))
#<FOO 101060512> is an instance of class #<Standard-Class FOO 101054510>:
 The following slots have :INSTANCE allocation:
 Z    2
 Y    1
 X    0

(add-slot (find-class 'foo) 'z '2 #'(lambda () 2))

(describe (make-instance 'foo))
#<FOO 101105512> is an instance of class #<Standard-Class FOO 101104233>:
 The following slots have :INSTANCE allocation:
 Z    2
 Y    1
 X    0