Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
exception safety / RAII ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-03-07 (18:47)
From: Michael Walter <>
Subject: Re: [Caml-list] Re: exception safety / RAII ?
On Mon, 7 Mar 2005 17:29:19 +0000, Jon Harrop <> wrote:
> On Monday 07 March 2005 15:21, Michael Walter wrote:
> > On Mon, 7 Mar 2005 13:30:48 +0000, Jon Harrop <> wrote:
> > > I don't know C#, but when would you want to deallocate a resource before
> > > all references to it have disappeared?
> >
> > I never had such a desire.
> Would you mind elaborating a little on what you do desire, i.e. what does
> "using" do in C#, when would you use it and how does this relate to OCaml?

Sure. I hope the following answers all three questions at once:

let using resource thunk =
      result = thunk resource
      dispose resource;
   any_exception ->
     dispose resource;
     raise any_exception

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.

> Presumably this is only difficult in the more complicated case of a general
> dependency graph between objects? In particular, one which has cycles. What
> kinds of programs require such sophistication?
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.