From: Xavier Leroy <xavier.leroy@inria.fr>
To: Blair Zajac <blair@orcaware.com>
Cc: Caml Users Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Resource acquisition is initialization
Date: Thu, 12 Dec 2002 14:15:06 +0100 [thread overview]
Message-ID: <20021212141506.A24877@pauillac.inria.fr> (raw)
In-Reply-To: <3DF78FB5.A1642B8@orcaware.com>; from blair@orcaware.com on Wed, Dec 11, 2002 at 11:19:17AM -0800
> 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.
> Also, since objects have initializers, do they have finializers? I
> read the entire Oreilly book and didn't see any mention of them.
> Reading the C code interface, it looks like you can associate a
> finalizer function to clean up an abstract type, but can you do
> this with normal Ocaml code?
Yes. The function Gc.finalise lets you attach finalization code to any
heap-allocated Caml value, not just objects. I'm not surprised it is
not mentioned in the O'Reilly book, since this is a recent addition to
the OCaml implementation.
- Xavier Leroy
-------------------
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
next prev parent reply other threads:[~2002-12-12 13:17 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-12-11 19:19 Blair Zajac
2002-12-11 19:55 ` Brian Hurt
2002-12-12 0:27 ` Chet Murthy
2002-12-12 7:56 ` Alessandro Baretta
2002-12-12 16:39 ` Brian Hurt
2002-12-13 9:22 ` Mike Potanin
2002-12-13 17:05 ` David Brown
2002-12-12 13:15 ` Xavier Leroy [this message]
2002-12-12 14:05 ` Dmitry Bely
2002-12-12 14:16 ` Xavier Leroy
2002-12-12 22:17 ` Quetzalcoatl Bradley
2002-12-12 23:59 ` Blair Zajac
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20021212141506.A24877@pauillac.inria.fr \
--to=xavier.leroy@inria.fr \
--cc=blair@orcaware.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox