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 TAA20756; Thu, 20 Nov 2003 19:00:14 +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 TAA20818 for ; Thu, 20 Nov 2003 19:00:11 +0100 (MET) Received: from mtaw6.prodigy.net (mtaw6.prodigy.net [64.164.98.56]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hAKI07126156 for ; Thu, 20 Nov 2003 19:00:10 +0100 (MET) Received: from lobus.fungible.com (adsl-64-161-114-2.dsl.snfc21.pacbell.net [64.161.114.2]) by mtaw6.prodigy.net (8.12.10/8.12.10) with ESMTP id hAKHxQVs027415; Thu, 20 Nov 2003 09:59:31 -0800 (PST) Received: by lobus.fungible.com (Postfix, from userid 1000) id 81A7F4BED8; Thu, 20 Nov 2003 09:59:59 -0800 (PST) Date: Thu, 20 Nov 2003 09:54:09 -0700 From-Tims-Fingers: true To: Christian.Schaller@siemens.com Cc: caml-list@inria.fr In-reply-to: (Christian.Schaller@siemens.com) Subject: Re: [Caml-list] closing file descriptors and channels References: Message-Id: <20031120175959.81A7F4BED8@lobus.fungible.com> From: tim@fungible.com (Tim Freeman) X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 fungible:01 schaller:01 schaller:01 java's:01 failwith:01 java's:01 fungible:01 ecdf:01 7180:01 relieves:99 terminator:01 handler:01 handler:01 descriptors:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk From: "Christian Schaller" >Yup, I've seen this one already. This one makes it complicated, since >for propagating the exception, I have to duplicate the closings: > > . > . > . > with > End_of_file -> output_string oc (line ^ "\n"); > Found line -> (close_out oc; close_in ic; raise Found line); > close_out oc; > close_in ic Java's "try ... finally" is useful and can be recreated in ML. Here's a definition: let finally (body: unit -> 'a) (handler: unit -> unit): 'a = let result: 'a option ref = ref None in begin begin try begin result := Some (body ()); end with e -> begin handler (); raise e; end; end; handler (); match !result with Some r -> r | _ -> failwith "Confused in finally"; end;; and here's how to use it for your example: let ic = "open the input file somehow" in finally (fun () -> let oc = "open the output file somehow" in finally (fun () -> . . (sometimes raise Found somewhere in here) . with End_of_file -> output_string oc "whatever") (fun () -> close_out oc)) (fun () -> close_in ic) I have to admit Java's syntax for try...finally is better. -- Tim Freeman tim@fungible.com GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D 7180 76DF FE00 34B1 5C78 Your levity is good. It relieves tension and fear of death. -- Terminator 3 ------------------- 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