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 TAA00883; Wed, 30 Jul 2003 19:25:46 +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 TAA32713 for ; Wed, 30 Jul 2003 19:25:45 +0200 (MET DST) Received: from morgon.inria.fr (morgon.inria.fr [128.93.8.33]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h6UHPPf25905; Wed, 30 Jul 2003 19:25:25 +0200 (MET DST) Received: (from remy@localhost) by morgon.inria.fr (8.11.6/8.11.6) id h6UHT5Q08593; Wed, 30 Jul 2003 19:29:05 +0200 Date: Wed, 30 Jul 2003 19:29:05 +0200 From: Didier Remy To: "Yaron M. Minsky" Cc: Caml List Subject: Re: [Caml-list] unwind-protect Message-ID: <20030730192905.A8578@morgon.inria.fr> Reply-To: Didier.Remy@inria.fr References: <4.3.2.7.2.20030728112622.02f3fef8@localhost> <3F275B5A.3010301@cs.caltech.edu> <1059561019.25904.1468.camel@dragonfly.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <1059561019.25904.1468.camel@dragonfly.localdomain>; from yminsky@cs.cornell.edu on Wed, Jul 30, 2003 at 06:30:19AM -0400 Organization: INRIA, BP 105, F-78153 Le Chesnay Cedex Phone: (33) 1 3963 5317 -- Sec: (33) 1 3963 5207 -- Fax: (33) 1 3963 5193 Web: http://cristal.inria.fr/~remy X-Loop: caml-list@inria.fr X-Spam: no; 0.00; didier:01 caml-list:01 downsides:01 tad:99 cleanup:01 finalization:01 semantics:01 remy:01 exception:02 computations:03 exn:03 trick:03 ensuring:04 let:04 parameter:04 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > 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. Yes, this is essential when the finalization code is a parameter. You do not need to use side effects, though. type 'a computation = Value of 'a | Exception of exn let freeze_computation f x = try Value (f x) with z -> Exception z let unfreeze_computation = function Value v -> v | Exception z -> raise z;; let try_finalize f x g y = let fx = freeze_computation f x in let _ = g y in unfreeze_computation fx There is still a choice in the semantics regarding which exception to return when both computations f x and g y fail. The above code reports the exception of the finalization code. To report the exception of the computation, replace let _ = g y in by let _ = freeze_computation g y in Didier ------------------- 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