From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 0496ABC32 for ; Tue, 8 Mar 2005 12:33:42 +0100 (CET) Received: from will.iki.fi (will.iki.fi [217.169.64.20]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j28BXfw3009774 for ; Tue, 8 Mar 2005 12:33:41 +0100 Received: from acerf.exomi.com (fa-3-0-0.fw.exomi.com [217.169.64.99]) by will.iki.fi (Postfix) with ESMTP id B9197137; Tue, 8 Mar 2005 13:33:34 +0200 (EET) Subject: Re: [Caml-list] Re: exception safety / RAII ? From: Ville-Pertti Keinonen To: Michael Walter Cc: caml-list@yquem.inria.fr, Jon Harrop In-Reply-To: <877e9a1705030710476502ad31@mail.gmail.com> References: <293072a520e3724a0497e6456a8675be@mac.com> <200503071330.49084.jon@jdh30.plus.com> <877e9a170503070721b2e0298@mail.gmail.com> <200503071729.20117.jon@jdh30.plus.com> <877e9a1705030710476502ad31@mail.gmail.com> Content-Type: text/plain Date: Tue, 08 Mar 2005 13:33:12 +0200 Message-Id: <1110281592.680.102.camel@localhost> Mime-Version: 1.0 X-Mailer: Evolution 2.0.4 FreeBSD GNOME Team Port Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 422D8D95.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 o'caml:01 ocaml:01 finalizers:01 o'caml:01 ocaml:01 imho:01 garbage:01 sockets:01 deallocated:01 afaik:01 wrote:01 exception:01 reasoning:01 lisp:01 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: On Mon, 2005-03-07 at 13:47 -0500, Michael Walter wrote: > My O'Caml is not very fluent, possible "dispose resource" should read > "resource # dispose". Basically, the idea is to deterministically > clean up resources, as early as possible (yes, "but it's not as early > as possible" is besides the point :-). In my experience this > simplifies resource management and reasoning about it. You're right, what you describe is often a good way of managing resources and also standard practice in e.g. Common Lisp (your "using" function looks a bit like unwind-protect). In OCaml you need to know what function to use for disposal of the specific kind of resource. For the most general approach, you can pass a function to call in order to dispose of the resource (making it even more like unwind-protect). > I have no idea about with finalizers in O'Caml (hence my more > broad/general statement), but in other languages I've worked with > there are several limitations which all basically origin in the fact > that finalization order in these languages was non-deterministic. This is also true in OCaml. The current languages that I'm aware of in which people rely on deterministic finalization are Python and Visual Basic. In both cases, it places nasty limitations on the implementation. IMHO currently the closest to the "best of both worlds" is a good garbage collector (with non-deterministic finalization) and explicit management for those resources that need them (file handles are a good example; in addition to the reasons already mentioned in this thread, also because they may refer to sockets and to avoid running into file descriptor limits). I'm not sure I understand Jon Harrop's concern about using resources after they've been deallocated. This has been addressed in the obvious way (return errors for operations on remaining references) in various languages for decades, and unlike memory management and type errors, AFAIK hasn't been a major source of bugs or complaints.