You can use with-output-to-string
for this. Here's an example:
(flet ((do-awesome-stuff-with-string (string)
(concatenate 'string string " is awesome!")))
(let ((err-string (with-output-to-string (s)
(write (make-instance 'error) :stream s))))
(do-awesome-stuff-with-string err-string)))
;; => "#<ERROR {25813951}> is awesome!"
Here's here's the HyperSpec entry on with-output-to-string
.
The reason (write (make-instance 'error) :stream nil)
doesn't work is that the :stream
argument to write
is a stream designator and in that context nil
is shorthand for *standard-output*
. (The fact that format
instead takes nil
to mean that it should return a string is a common point of confusion).