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: 2002-01-24 (15:52)
From: Doug Bagley <doug@b...>
Subject: Re: [Caml-list] beginner question about camlp4
Charles Martin wrote:
> 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)

Are you sure that's right?  I think (f chan) executes before the call
to protect, and you want protect to execute f:

let with_input_file fname f =
  let chan = open_in fname in
  protect (fun () -> f chan)
     (fun () -> prerr_endline ("closing " ^ fname); close_in chan)


I have also developed similar "with_input_from" kind of functions, but
I use Unix.dup/dup2 to switch out the filedescriptors so the functions
that run under them can just read/write from stdin/stdout, instead of
needing a specific channel.  Seems like a common idiom, is it from
lisp?  I forget.  You're welcome to look at my versions here:

Of course, maybe I'm the one who is not doing it right.

Bug reports:  FAQ:
To unsubscribe, mail  Archives: