From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id D4369BC75 for ; Sat, 5 Mar 2005 19:16:45 +0100 (CET) Received: from smtpout.mac.com (smtpout.mac.com [17.250.248.70]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j25IGiN1015860 for ; Sat, 5 Mar 2005 19:16:45 +0100 Received: from mac.com (smtpin02-en2 [10.13.10.147]) by smtpout.mac.com (Xserve/8.12.11/MantshX 4.0) with ESMTP id j25IGhiP007272 for ; Sat, 5 Mar 2005 10:16:43 -0800 (PST) Received: from [192.168.1.100] (clt74-96-052.carolina.rr.com [24.74.96.52]) (authenticated bits=0) by mac.com (Xserve/smtpin02/MantshX 4.0) with ESMTP id j25IGgLr028521 for ; Sat, 5 Mar 2005 10:16:43 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v619.2) Content-Transfer-Encoding: 7bit Message-Id: <293072a520e3724a0497e6456a8675be@mac.com> Content-Type: text/plain; charset=US-ASCII; format=flowed To: caml-list@yquem.inria.fr From: Michael Benfield Subject: exception safety / RAII ? Date: Sat, 5 Mar 2005 13:16:41 -0500 X-Mailer: Apple Mail (2.619.2) X-Miltered: at concorde with ID 4229F78C.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; ocaml:01 deallocated:01 ocaml:01 exception:01 exception:01 exceptions:01 exceptions:01 functions:01 scope:01 finalization:01 finalization:01 objects:02 exit:04 exit:04 problem:05 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: I'm looking at OCaml coming from sort of a C++ background and I'm finding it really exciting. There's one thing that worries me though. C++ programmers have been dealing with issues of exception safety for years - it's a complicated problem because coding in the presence of exceptions for all intents and purposes means your function could end at any point, so how can you make sure resources are deallocated? The C++ solution to this problem is a technique called Resource Acquisition Is Initialization. C++ objects have destructors, which are simply functions that will always be called on exit from a scope - including if the exit is caused by an exception coming up through your function. You make resource release (whether the resource is memory, a socket, whatever) happen in a destructor, and then you are set. This is very handy even disregarding exceptions. So I'm just wondering what facilities OCaml has to either implement this concept, or other concepts to help with exception safety? The OCaml manual says: "Also, finalization can be performed by trapping all exceptions, performing the finalization, then raising again the exception". This makes me cringe.