From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: weis Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA21716 for caml-redistribution@pauillac.inria.fr; Mon, 7 Feb 2000 19:41:55 +0100 (MET) Resent-Message-Id: <200002071841.TAA21716@pauillac.inria.fr> 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 QAA03925 for ; Mon, 7 Feb 2000 16:09:15 +0100 (MET) Received: from cepheus.azstarnet.com (cepheus.azstarnet.com [169.197.56.195]) by concorde.inria.fr (8.8.7/8.8.7) with ESMTP id QAA04197 for ; Mon, 7 Feb 2000 16:09:10 +0100 (MET) Received: from vega (dialup05ip107.tus.azstarnet.com [169.197.32.107]) by cepheus.azstarnet.com (8.9.3+blt.Beta0/8.9.3) with SMTP id IAA19159 for ; Mon, 7 Feb 2000 08:08:54 -0700 (MST) X-Sent-via: StarNet http://www.azstarnet.com/ Message-ID: <000b01bf717d$4534c480$250148bf@vega> From: "David McClain" To: Subject: File I/O and Finalization Date: Mon, 7 Feb 2000 08:08:58 -0700 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.00.2615.200 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Resent-From: weis@pauillac.inria.fr Resent-Date: Mon, 7 Feb 2000 19:41:54 +0100 Resent-To: caml-redistribution@pauillac.inria.fr Finalization is not really required here... I have implemented a very simple version of the Lisp UNWIND-PROTECT mechanism to handle the automatic closing of files: let unwind_protect fn_do fn_exit = let rslt = try fn() with exn -> ignore(exitfn()); raise exn in ignore(exitfn()); rslt then one can define a safe I/O operation on files as: let with_file filename fn = let fp = open_in filename in unwind_protect (fun () -> fn fp) (fun () -> close_in fp) This allows the function fn to operate on the opened file and it guarantees that the file gets closed regardless of how the function exits (normally, or abnormally). D.McClain