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 PAA06740; Thu, 12 Dec 2002 15:07:03 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 PAA06710 for ; Thu, 12 Dec 2002 15:07:02 +0100 (MET) Received: from relay.rinet.ru (relay.rinet.ru [195.54.192.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id gBCE71X12930 for ; Thu, 12 Dec 2002 15:07:02 +0100 (MET) Received: from relay.rinet.ru (localhost [127.0.0.1]) by relay.rinet.ru (8.12.6/8.12.6) with ESMTP id gBCE70xc082714 for ; Thu, 12 Dec 2002 17:07:00 +0300 (MSK) (envelope-from dbely@mail.ru) Received: (from uucp@localhost) by relay.rinet.ru (8.12.6/8.12.6/Submit) with UUCP id gBCE70EE082713 for caml-list@inria.fr; Thu, 12 Dec 2002 17:07:00 +0300 (MSK) Received: from dialin3.stormoff (ROVER1) [192.168.0.131] by stormoff with esmtp (Exim 3.12 #1 (Debian)) id 18MTzD-0007Qz-00; Thu, 12 Dec 2002 17:06:48 +0300 X-Comment-To: Xavier Leroy To: caml-list@inria.fr Subject: Re: [Caml-list] Resource acquisition is initialization References: <3DF78FB5.A1642B8@orcaware.com> <20021212141506.A24877@pauillac.inria.fr> From: Dmitry Bely Date: Thu, 12 Dec 2002 17:05:42 +0300 In-Reply-To: <20021212141506.A24877@pauillac.inria.fr> (Xavier Leroy's message of "Thu, 12 Dec 2002 14:15:06 +0100") Message-ID: User-Agent: Gnus/5.090005 (Oort Gnus v0.05) XEmacs/21.4 (Military Intelligence (RC5 Windows), i586-pc-win32) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Xavier Leroy writes: >> 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 Maybe I don't understand something but IMHO it's not very usable because with_file_in type is fixed after the first use. So e.g. input_line and input_char operation cannot be mixed. You cannot write something like let process filename = let f = with_file_in filename in begin print_string (f input_line); print_char (f input_char); (* syntax error! *) end - Dmitry Bely ------------------- 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