;;; CLOS 5: WITH-SLOTS AND WITH-ACCESSORS ;;; ;;; Slot access through POINT-X and POINT-Y works, but spells the ;;; name out every time. WITH-SLOTS reads the slots as plain ;;; variables: ;;; ;;; (with-slots (x y) p (list x y)) ;;; ;;; WITH-ACCESSORS does the same through the accessor names, so ;;; SETF goes through the accessor too: ;;; ;;; (with-accessors ((x point-x)) p (setf x (1+ x))) (defclass point () ((x :initarg :x :accessor point-x) (y :initarg :y :accessor point-y))) ;; TODO: The point P with both coordinates doubled - a NEW point. (defun double-point (p) (make-instance 'point :x (* 2 (point-x p)) :y (point-y p))) ;; TODO: P with its X stepped up by one - the SAME point, changed. (defun shift-x (p) (setf (point-x p) (+ 2 (point-x p))) p) ;;; ------------------------------------------------------------------ ;;; Checks. Read them - they tell you what to aim for - but leave ;;; them unchanged. ;;; ------------------------------------------------------------------ (check (let ((q (double-point (make-instance 'point :x 3 :y 4)))) (and (eql 6 (point-x q)) (eql 8 (point-y q))))) (check (let ((p (shift-x (make-instance 'point :x 1)))) (eql 2 (point-x p)))) (check (let ((p (shift-x (make-instance 'point :x 10)))) (eql 11 (point-x p))))