* File I/O and Finalization
@ 2000-02-07 15:08 David McClain
0 siblings, 0 replies; only message in thread
From: David McClain @ 2000-02-07 15:08 UTC (permalink / raw)
To: caml-list
Finalization is not really required here... I have implemented a very simple
version of the Lisp UNWIND-PROTECT mechanism to handle the automatic closing
of files:
let unwind_protect fn_do fn_exit =
let rslt =
try
fn()
with
exn ->
ignore(exitfn());
raise exn
in
ignore(exitfn());
rslt
then one can define a safe I/O operation on files as:
let with_file filename fn =
let fp = open_in filename in
unwind_protect
(fun () -> fn fp)
(fun () -> close_in fp)
This allows the function fn to operate on the opened file and it guarantees
that the file gets closed regardless of how the function exits (normally, or
abnormally).
D.McClain
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2000-02-07 18:41 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-07 15:08 File I/O and Finalization David McClain
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox