As an addition to ddyer, you can also use MACROEXPAND or MACROEXPAND-1 to find out what WITH-OPEN-FILE does:
(macroexpand '(with-open-file (out filename :direction :output :if-exists :supersede) (with-standard-io-syntax (print *db* out))))
informs us
(LET ((OUT (OPEN FILENAME :DIRECTION :OUTPUT :IF-EXISTS :SUPERSEDE)) (#:G748 T)) (UNWIND-PROTECT (MULTIPLE-VALUE-PROG1 (PROGN (WITH-STANDARD-IO-SYNTAX (PRINT *DB* OUT))) (SETQ #:G748 NIL)) (WHEN OUT (CLOSE OUT :ABORT #:G748))))
We see that we open a file with the name filename and assign this open file stream out and do something. If something bad UNWIND-PROTECT , UNWIND-PROTECT will CLOSE thread if it was not nil .
Variable #:G748 is a GENSYM ed character (so this is a new, uninterrupted character). If all else fails, write a file, we will set #:G748 to nil .
Thus, when we CLOSE stream, if something went wrong, #:G748 will be T , so CLOSE will try to clear any side effects from creating the stream.
Frank shearar
source share