Skip to content

Instantly share code, notes, and snippets.

@spacebat
Last active July 1, 2019 01:04
Show Gist options
  • Save spacebat/ab88d3e57f4c83ebd7f55a6558bfc5ce to your computer and use it in GitHub Desktop.
Save spacebat/ab88d3e57f4c83ebd7f55a6558bfc5ce to your computer and use it in GitHub Desktop.
(ql:quickload :closer-mop)
(defclass foo ()
((bar :initarg :bar :initform nil :reader foo-bar)))
(defclass fuu (foo)
((baz :initarg :baz :initform nil :reader fuu-baz)))
(defmethod print-object ((f foo) stream)
(print-unreadable-object (f stream :type t)))
(make-instance 'fuu :bar "barry" :baz "bazza")
;; => #<FUU >
(defmethod print-object :around ((f foo) stream)
(let* ((slots (closer-mop:class-slots (class-of f)))
(slot-names (mapcar #'closer-mop:slot-definition-name slots))
(output (let ((s (make-string-output-stream)))
(call-next-method f s)
(get-output-stream-string s)))
(last-char (aref output (- (length output) 1)))
(new-output (subseq output 0 (- (length output) 1))))
(write-string new-output stream)
(format stream "~{~?~^ ~}"
(loop for name in slot-names
collect ":~(~A~) ~S"
collect (list name (slot-value f name))))
(write-char last-char stream)))
(make-instance 'fuu :bar "barry" :baz "bazza")
;; => #<FUU :bar "barry" :baz "bazza">
@fiddlerwoaroof
Copy link

fiddlerwoaroof commented Jun 29, 2019

If you replace SB-MOP: with CLOSER-MOP:, (and add a dependency on CLOSER-MOP), this should work on almost all implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment