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 AAA23274; Fri, 13 Dec 2002 00:59:11 +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 AAA23640 for ; Fri, 13 Dec 2002 00:59:10 +0100 (MET) Received: from orcaware.com (bdsl.66.12.233.174.gte.net [66.12.233.174]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id gBCNx8115707; Fri, 13 Dec 2002 00:59:08 +0100 (MET) Received: from orcaware.com (localhost.localdomain [127.0.0.1]) by orcaware.com (8.12.5/8.12.5) with ESMTP id gBCNx69O007750; Thu, 12 Dec 2002 15:59:06 -0800 Message-ID: <3DF922E0.AA7F3D82@orcaware.com> Date: Thu, 12 Dec 2002 15:59:28 -0800 From: Blair Zajac X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U) X-Accept-Language: en,x-ns1rfacHe4WNh5,x-ns2U100btwUq5f MIME-Version: 1.0 To: Xavier Leroy CC: Caml Users Mailing List Subject: Re: [Caml-list] Resource acquisition is initialization References: <3DF78FB5.A1642B8@orcaware.com> <20021212141506.A24877@pauillac.inria.fr> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Xavier Leroy wrote: > > > One of the nice things about C++ and Java is that with properly > > designed classes, you don't need to worry about freeing resources > > in complicated code, because the when the objects go out of scope > > either normally or via an exception, they will clean themselves up. > > I believe this is a C++-specific idiom. Java doesn't have > destructors, just finalizers that are called asynchronously by the > GC. OCaml also has GC finalization (see below). > > > Given that Ocaml has objects, it would be useful to have this > > idiom available to us. Is there a way to implement it, rather > > than just waiting for the garbage collector? > > Yes: higher-order functions. For a file: > > let with_file_in filename action = > let ic = open_in filename in > try > let res = action ic in close_in ic; res > with x -> > close_in ic; raise x > > For a mutex: > > let synchronize mut action = > try > Mutex.lock mut; > let res = action () in > Mutex.unlock mut; > res > with x -> > Mutex.unlock mut; > raise x > > You get the idea. Thanks for all the answers everybody sent, both on and off list, it's much appreciated. Best, Blair -- Blair Zajac Plots of your system's performance - http://www.orcaware.com/orca/ ------------------- 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