Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] beginner question about camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Charles Martin <joelisp@y...>
Subject: Re: [Caml-list] beginner question about camlp4

>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 -> [...] ))


Do You Yahoo!?
Get your free address at

Bug reports:  FAQ:
To unsubscribe, mail  Archives: