Version française
Home     About     Download     Resources     Contact us    
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: 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:

 http://www.bagley.org/~doug/ocaml/io_lib/

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

cheers,
doug
-------------------
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