In Common Lisp, how can I override the default string representation of a CLOS class so that calls to format
or princ
will print something intelligible, even when objects of that class are embedded within other types, such as lists or arrays?
For example, if I call (format t "~a~%" x)
when x holds an instance of my solution class, I want it to print something like #<SOLUTION genes: #(1 2 3) scores: #(4 5) rank: 6>
instead of #<SOLUTION {BB7CD31}>
.
So far, all I have managed to figure out is writing custom functions to handle printing structures that I know will contain instances of this class, but this is tedious. Surely Lisp provides some way to get this functionality for free?
format
, but it seems strange to call the function that may very well be calling this method. – Judo