Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
From: Michael Walter <michael.walter@g...>
Subject: Re: [Caml-list] Re: exception safety / RAII ?
On Mon, 7 Mar 2005 17:29:19 +0000, Jon Harrop <jon@jdh30.plus.com> wrote:
> On Monday 07 March 2005 15:21, Michael Walter wrote:
> > On Mon, 7 Mar 2005 13:30:48 +0000, Jon Harrop <jon@jdh30.plus.com> 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 =
  try
    let
      result = thunk resource
    in
      dispose resource;
      result
  with
   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.

Greetings,
Michael