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

How to add a new slot-option



I would like to add a new slot-option.  Is there a sanctioned way to do 
this?

For example suppose that I had a vehicle database interfaced to CL.  Below 
is a defclass whose weight slot is to be initialized from the database.

(defclass vehicle
    ()
    ((make
	:initarg :make
	:read make
	:type symbol)
     (model
	:initarg :model
	:reader model
	:type symbol)
     (WEIGHT
	:INITRETRIEVE 
	(lambda (&rest initargs &key make model)
	  (declare (ignore initargs))
	  (setf make (string make)
		model (string model))
	  (retrieve '(column vehicle weight)
		    :where `(and (string= ,make (column vehicle make))
				 (string= ,model (column vehicle model)))))
	:reader weight
	:type integer
	:documentation "This slot is initialized to the value stored in a 
database.  The vehicle's make and model are used to qualify the desired 
record.")
     ))


(defmethod shared-initialize :around 
	   ((instance vehicle) slot-names &rest initargs)
  ;; loop over all the slot instances, adding initargs and values for
  ;; all slots whose :INITRETRIEVE slot-option is not nil.
  (dolist (slot (pcl::class-slots (class-of instance)))
    (when (not (null (initretrieve slot)))
      (setf initargs (apply (list (first (initargs slot)) 
				  (apply (initretrieve slot) initargs))
			    initargs))))
  (call-next-method instance slot-names initargs))


I cannot use the slot weight's :initform because its initial value 
depends upon the initial values of slots make and model.


Thanks for any help,
Darrell Shane
To: CommonLoops.PA@Xerox.COM
Subject: How to add a new slot-option
--------
I would like to add a new slot-option.  Is there a sanctioned way to do 
this?

For example suppose that I had a vehicle database interfaced to CL.  Below 
is a defclass whose weight slot is to be initialized from the database.

(defclass vehicle
    ()
    ((make
	:initarg :make
	:read make
	:type symbol)
     (model
	:initarg :model
	:reader model
	:type symbol)
     (WEIGHT
	:INITRETRIEVE 
	(lambda (&rest initargs &key make model)
	  (declare (ignore initargs))
	  (setf make (string make)
		model (string model))
	  (retrieve '(column vehicle weight)
		    :where `(and (string= ,make (column vehicle make))
				 (string= ,model (column vehicle model)))))
	:reader weight
	:type integer
	:documentation "This slot is initialized to the value stored in a 
database.  The vehicle's make and model are used to qualify the desired 
record.")
     ))


(defmethod shared-initialize :around 
	   ((instance vehicle) slot-names &rest initargs)
  ;; loop over all the slot instances, adding initargs and values for
  ;; all slots whose :INITRETRIEVE slot-option is not nil.
  (dolist (slot (pcl::class-slots (class-of instance)))
    (when (not (null (initretrieve slot)))
      (setf initargs (apply (list (first (initargs slot)) 
				  (apply (initretrieve slot) initargs))
			    initargs))))
  (call-next-method instance slot-names initargs))


I cannot use the slot weight's :initform because its initial value 
depends upon the initial values of slots make and model.


Thanks for any help,
Darrell Shane