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

CLOS With-Accessors

I have a couple of queries about the use of With-Accessors in CLOS
that I would appreciate some help with.
Perhaps the easiest way of explaining my queries is with some example code
and asking if the code is legal, and if not why not.

Example 1:

(defclass A ()
    ((a-slot :initform 0)
     (a-slot2 :initform 0 :reader a-slot2 )))

;;; Always lie about what's in a-slot
(defmethod a-slot ((A A))
  (* (slot-value A 'a-slot) 0.9))

;;; Better stay consistent
(defmethod (setf a-slot) (value (A A))
  (setf (slot-value A 'a-slot) (* value (/ .9)))

;;; Now use WITH-ACCESSORS with a home-brew accessor 
(defmethod foo ((A A))
  (with-accessors ((a-slot a-slot)) A
    (incf a-slot 5)))

(defmethod foo2 ((A A))
  (with-accessors ((a-slot2 a-slot2)) A

Is the method FOO valid since it isn't using system defined accessors?
Is the method FOO2 valid, since A-SLOT2 is only a reader method, but I'm only using
the 'accessor' in reader mode?

Example 2:

(defclass B ()
    ((b-slot :initform 1 :accessor b-slot)))

;;; Class C REQUIRES that Class B will be mixed in when any C's are instantiated
(defclass C ()
    ((c-slot :initform 2 :accessor c-slot )))

(defmethod bar ((C C))
  (with-accessors ((b-slot b-slot)) C
    (incf b-slot)))

(defmethod baz ((C C))
  (with-slots (b-slot) C
    (incf b-slot)))

(defclass D (B C)

Are the methods BAR and BAZ legal, i.e. can With-Accessors and With-Slots be used 
with accessors/slots that won't be available until run-time?

All the above compiles and runs as I would hope on a Symbolics, but is this portable?
Is any of this implementation dependent?

Thanks in advance for any help that may be forthcoming,
Guy Footring.