Last active
July 1, 2019 01:04
-
-
Save spacebat/ab88d3e57f4c83ebd7f55a6558bfc5ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you replace
SB-MOP:
withCLOSER-MOP:
, (and add a dependency on CLOSER-MOP), this should work on almost all implementations.