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 MAA17121; Wed, 30 Jul 2003 12:30:23 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id MAA26940 for ; Wed, 30 Jul 2003 12:30:21 +0200 (MET DST) Received: from tisch.mail.mindspring.net (tisch.mail.mindspring.net [207.69.200.157]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h6UAULf02214 for ; Wed, 30 Jul 2003 12:30:21 +0200 (MET DST) Received: from user-0ccekej.cable.mindspring.com ([24.199.81.211] helo=[192.168.0.3]) by tisch.mail.mindspring.net with esmtp (Exim 3.33 #1) id 19hoDs-0008Af-00 for caml-list@inria.fr; Wed, 30 Jul 2003 06:30:20 -0400 Subject: Re: [Caml-list] unwind-protect From: "Yaron M. Minsky" To: Caml List In-Reply-To: <3F275B5A.3010301@cs.caltech.edu> References: <4.3.2.7.2.20030728112622.02f3fef8@localhost> <3F275B5A.3010301@cs.caltech.edu> Content-Type: text/plain Organization: Cornell University Message-Id: <1059561019.25904.1468.camel@dragonfly.localdomain> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.2 (1.2.2-5) Date: 30 Jul 2003 06:30:19 -0400 Content-Transfer-Encoding: 7bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; yaron:01 minsky:01 yminsky:01 cornell:01 caml-list:01 downsides:01 tad:99 cleanup:01 newbies:01 44,:01 hickey:01 pragmatic:01 camlp:01 re-raising:01 interfere:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk I've more or less given up on this feature ever finding it's way into the language, and I ended up writing my own, like many others. One of the downsides of having people write their own unwind-protect's is that it's a tad tricky to get right. The trick is ensuring that the cleanup code only gets run once, even if an exception is thrown in the middle of the cleanup. Here's my shot at it. let protect ~f ~(finally: unit -> unit) = let result = ref None in try result := Some (f ()); raise Exit with Exit as e -> finally (); (match !result with Some x -> x | None -> raise e) | e -> finally (); raise e I definitely agree it would be nice to have this kind of thing in the language, or at least in the standard library. When we give newbies examples of how to read a file, giving them an example that uses unwind-protect would prevent many future file-descriptor-exhaustion bugs. y On Wed, 2003-07-30 at 01:44, Jason Hickey wrote: > A related, but pragmatic problem. Suppose we have some imperative code, > and we need an "unwind-protect" operation. Consider this code to make > sure a file is closed after it is used. > > let with_open_file (name : string) (f : in_channel -> unit) = > let inc = open_in name in > try > f inc; > close inc > with > exn -> > close inc; > raise exn > > 1. It would be wonderful to have a "finally" clause, so we don't have to > duplicate the close operation. I suppose we can fake it with CamlP4, > but it seems better built-in. > > 2. This kind of exception re-raising does not work well with stack > tracing (OCAMLRUNPARAM=b). It would be very nice to add a "finally" > clause that did not interfere with stack tracing, or make it so that > re-raising would preserve the location where the exception was created. > > Jason -- |--------/ Yaron M. Minsky \--------| |--------\ http://www.cs.cornell.edu/home/yminsky/ /--------| Open PGP --- KeyID B1FFD916 (new key as of Dec 4th) Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916 ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners