Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Charles Martin <joelisp@yahoo.com>
To: Doug Bagley <doug@bagley.org>, Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] beginner question about camlp4
Date: Wed, 23 Jan 2002 11:43:27 -0800	[thread overview]
Message-ID: <5.1.0.14.0.20020123113751.022f34c0@pop.mail.yahoo.com> (raw)
In-Reply-To: <m38zapkcp5.fsf@ns.bagley.org>


>P.S. Incidentally, before I tried camlp4, I had come up with a HOF to
>encapsulate cleanup for exceptions, like this:
>
>let safely setup cleanup subject f =
>  let x = setup subject in
>  try f x; cleanup x with e -> cleanup x; raise e
>
>But in complicated situations, using this function becomes a little
>hard to follow, like if I'm nesting a few file opens.

I think it would be easier if you broke your HOFs down slightly differently.  I use the following "base" HOF:

let protect f always =
  try let r = f () in always (); r
  with e -> always (); raise e

For file handling, I then use the following:

let with_input_file fname f =
  let chan = open_in fname in
  protect (f chan) (fun () -> close_in chan)

With a similar "with_output_file".  Typical code then looks like this:

let foo infile outfile =
  with_input_file infile
    (fun inchan -> with_output_file outfile
      (fun outchan -> [...] ))

Charles


_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  parent reply	other threads:[~2002-01-23 19:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-23  2:45 Doug Bagley
2002-01-23 10:15 ` Daniel de Rauglaudre
2002-01-23 19:43 ` Charles Martin [this message]
2002-01-23 20:00   ` Doug Bagley
2002-01-23 21:48     ` Charles Martin

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=5.1.0.14.0.20020123113751.022f34c0@pop.mail.yahoo.com \
    --to=joelisp@yahoo.com \
    --cc=caml-list@inria.fr \
    --cc=doug@bagley.org \
    /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