Source of Nothingness - 2009-03-21 :

* 2009-03-21 :

print-objectを定義するとprincとprin1の両方に影響するんだなぁ
別々に定義することはできないんだろうか。

と思ったらできた。
princとprin1の差は*print-escape*に現れるので、この値で分岐すれば良かった。*print-readably*かと勘違いしてた。

(defclass point ()
  ((x :initform (random 100))
   (y :initform (random 100))))
(make-instance 'point)
;=> #<POINT {121573B1}>
(defmethod print-object ((object point) stream)
  (if *print-escape*
      (print-unreadable-object (object stream :type t)
        (format stream "(~D, ~D)" (slot-value object 'x) (slot-value object 'y)))
      (format stream "(~D, ~D)" (slot-value object 'x) (slot-value object 'y))))
(make-instance 'point)
;=> #<POINT (53, 69)>
(format nil "~A" (make-instance 'point))
;=> "(55, 81)"
(format nil "~S" (make-instance 'point))
;=> "#<POINT (41, 71)>"

(defclass point ()
  ((x :initform (random 100))
   (y :initform (random 100))))
(make-instance 'point)
;=> #<POINT {122403B1}>
(defmethod print-object ((object point) stream)
  (print-unreadable-object (object stream :type t)
    (format stream "(~D, ~D)" (slot-value object 'x) (slot-value object 'y))))
(make-instance 'point)
;=> #<POINT (88, 12)>
(format nil "~A" (make-instance 'point))
;=> "#<POINT (98, 53)>"
(format nil "~S" (make-instance 'point))
;=> "#<POINT (88, 53)>"

update : 2009-03-22 (Sun) 00:49:22