From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id UAA22830; Wed, 23 Jan 2002 20:39:22 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA22488 for ; Wed, 23 Jan 2002 20:39:21 +0100 (MET) Received: from smtp017.mail.yahoo.com (smtp017.mail.yahoo.com [216.136.174.114]) by nez-perce.inria.fr (8.11.1/8.11.1) with SMTP id g0NJdJf25959 for ; Wed, 23 Jan 2002 20:39:20 +0100 (MET) Received: from adsl-63-195-80-23.dsl.snfc21.pacbell.net (HELO aria.yahoo.com) (63.195.80.23) by smtp.mail.vip.sc5.yahoo.com with SMTP; 23 Jan 2002 19:39:18 -0000 Message-Id: <5.1.0.14.0.20020123113751.022f34c0@pop.mail.yahoo.com> X-Sender: joelisp@pop.mail.yahoo.com X-Mailer: QUALCOMM Windows Eudora Version 5.1 Date: Wed, 23 Jan 2002 11:43:27 -0800 To: Doug Bagley , Caml List From: Charles Martin Subject: Re: [Caml-list] beginner question about camlp4 In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk >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