Mario S.Mommer <m_mommer@...> writes:
> I would sugest that the following is a bug, if not technically, then
> at least, shall we say, morally. Or at least, I think it would be a
> lot better if the behaviour was different.
> Consider the function
> (defun lose-important-and-expensive-data-please ()
> (with-open-file (strm "important-data.txt"
> :direction :output
> :if-exists :supersede)
> (format strm "42~%")
> (finish-output strm)
> Now, if I return to top level, the file "important-data.txt" gets
> _removed_, together with The Answer.
> This is particularily insidious if after a very long computation
> something breaks, but you would like to keep the log. Of course, I
> could just have copied the file somewhere else before pressing 'q',
> among a variety of other workarounds. But I think with-open-file
> shouldn't remove data just because. If anything, it makes the scenario
> of inadvertent data loss a lot more likely.
Well, you might have to take this up with the ANSI standardization
committee, because the CLHS page for WITH-OPEN-FILE says
| When control leaves the body, either normally or abnormally (such as
| by use of throw), the file is automatically closed. If a new output
| file is being written, and control leaves abnormally, the file is
| aborted and the file system is left, so far as possible, as if the
| file had never been opened.
So, if you don't want that behaviour from with-open-file, I think
you'd better not use with-open-file in the first place.
(This is not to disagree with your contention that this is a moral
bug, nor to agree with it particularly: just to point out that the
spec not only encourages but mandates the behaviour you've seen)