Why don't people like lisp?
mdanish at andrew.cmu.edu
Fri Oct 24 22:01:00 CEST 2003
On Fri, Oct 24, 2003 at 06:09:55PM +0000, Rainer Deyke wrote:
> [...] and is closed at the end of the same block (instead of, for
> example, when it is no longer needed).
The reasoning behind WITH-OPEN-FILE is that file streams are resources
that should be allocated with dynamic extent, not indefinite. To make
that more concrete, it means that file streams should be closed
immediately after they are no longer being used (much like local
variables in C are immediately destroyed when the function returns).
I have seen debates before that argue that memory should be the only
resource managed by GC, and the opposite view that every resource should
be managable by GC (I don't think this is quite as feasible, but you can
look up the discussions on google, a year ago on c.l.l or so).
But WITH-OPEN-FILE doesn't prevent you from, say, having a file open the
entire time your program is running. Some people pointed out that you
can use OPEN and CLOSE, but also this would work fine:
(defvar *log-stream*) ;; an unbound special variable
(defun log (fmt-control &rest args)
(apply 'format *log-stream* fmt-control args))
(defun start (&key (log-file "log"))
(with-open-file (*log-stream* log-file
;; *log-stream* is bound until this body exits
; Matthew Danish <mdanish at andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."
More information about the Python-list