From: Joshua Taylor
Subject: avoiding extra spaces with print-unreadable-object
Date: 
Message-ID: <66639d31-d2c3-4623-b9f9-ad2d592df138@f47g2000hsd.googlegroups.com>
Hello all,

This isn't anything too difficult to work around, but I'm wondering if
there's a better solution that I haven't seen yet.

I've got a class foo

(defclass foo ()
  ((bar :initarg :bar)))

and I'd like to specialize print-object for foo. It will be
unreadable, so I'll use print-unreadable-object. I want to print the
type of the instance. If bar is not bound, I want to print the
identity of the object, but if bar is bound, I'll print its value. The
obvious way (to me) is:

(defmethod print-object ((foo foo) *standard-output*)
  (print-unreadable-object (foo *standard-output*
			    :type t
			    :identity (not (slot-boundp foo 'bar)))
    (when (slot-boundp foo 'bar)
      (write (slot-value foo 'bar)))))

but this has the disadvantage that in the case that the identity is
printed, there are /two/ spaces between foo and the identity. The
following gets the behavior I'm looking for:


(defmethod print-object ((foo foo) *standard-output*)
  (if (slot-boundp foo 'bar)
      (print-unreadable-object (foo *standard-output* :type t)
	(write (slot-value foo 'bar)))
      (print-unreadable-object (foo *standard-output* :type
t :identity t))))

but the code a little ugly because of the two print-unreadable-object
forms. Any thoughts?

(As I say, this isn't a big issue; I'm just hoping to learn some nice
trick or idiom from some soul wiser than I.)

Thanks!
//J