caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "David McClain" <dmcclain@azstarnet.com>
To: <caml-list@pauillac.inria.fr>
Subject: File I/O and Finalization
Date: Mon, 7 Feb 2000 08:08:58 -0700	[thread overview]
Message-ID: <000b01bf717d$4534c480$250148bf@vega> (raw)

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





                 reply	other threads:[~2000-02-07 18:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='000b01bf717d$4534c480$250148bf@vega' \
    --to=dmcclain@azstarnet.com \
    --cc=caml-list@pauillac.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).