Files
lisplings/solutions/15_clos/clos7.lisp
T

25 lines
896 B
Common Lisp

;;; CLOS 7: PRINT-OBJECT AND DEFAULT-INITARGS (solution)
(defstruct candy filling size)
(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y))
(:default-initargs :x 0 :y 0))
(defmethod print-object ((p point) stream)
(print-unreadable-object (p stream :type t)
(format stream "x=~a y=~a" (point-x p) (point-y p))))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal 'swirl (candy-filling (make-candy :filling 'swirl)))
(check-equal 0 (point-x (make-instance 'point)))
(check-equal 7 (point-x (make-instance 'point :x 7)))
(check-equal "#<POINT x=3 y=4>"
(with-output-to-string (s)
(princ (make-instance 'point :x 3 :y 4) s)))