From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA09981 for caml-red; Fri, 17 Nov 2000 13:31:30 +0100 (MET) 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 NAA12934 for ; Fri, 17 Nov 2000 13:28:48 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id eAHCSkj13158; Fri, 17 Nov 2000 13:28:46 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id NAA21785; Fri, 17 Nov 2000 13:28:46 +0100 (MET) From: Pierre Weis Message-Id: <200011171228.NAA21785@pauillac.inria.fr> Subject: Re: unwind-protect? In-Reply-To: <87d7fvruas.church.of.emacs@meta.verbum.org> from Colin Walters at "Nov 17, 100 01:23:23 am" To: walters@cis.ohio-state.edu (Colin Walters) Date: Fri, 17 Nov 2000 13:28:46 +0100 (MET) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis@pauillac.inria.fr > Hello, > > Is there an equivalent of Lisp's `unwind-protect' or Java's > "try...finally"? I wanted to write a function which changed to > another directory temporarily, and I came up with: > > let in_directory d f = > let olddir = Unix.getcwd() in > (Unix.chdir d; > f(); > Unix.chdir olddir) > > But this won't work if f throws an exception. Any suggestions? There is no special construct: the basic construct is powerful enough to handle the construction: you just have to use pattern matching to bound the exception and raise it again, after having properly set up the global state. let in_directory d f = let olddir = Unix.getcwd () in try Unix.chdir d; f (); Unix.chdir olddir with x -> Unix.chdir olddir; raise x;; val in_directory : string -> (unit -> 'a) -> unit = Note also that this scheme can be generalized to cases where you need to apply a function to some argument and need to get the result of that call: let in_directory d f arg = let olddir = Unix.getcwd () in try Unix.chdir d; let r = f arg in Unix.chdir olddir; r with x -> Unix.chdir olddir; raise x;; val in_directory : string -> ('a -> 'b) -> 'a -> 'b = Here, polymorphism, curryfication, and specialization are shining, since you juste have to add one in_* functional application before an actual call to any function f to obtain a specialized evaluation context for f. For instance: let in_tmp f arg = in_directory "/tmp" f arg;; val in_tmp : ('a -> 'b) -> 'a -> 'b = let open_out_tmp fname = in_tmp open_out fname;; val open_out_tmp : string -> out_channel = Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/