Version française
Home     About     Download     Resources     Contact us    
Browse thread
[OSR] OCaml Standard Recommandation Process
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] [OSR] OCaml Standard Recommandation Process
On Jan 28, 2008 5:45 PM, Christophe TROESTLER
<Christophe.Troestler+ocaml@umh.ac.be> wrote:
>
> On Mon, 28 Jan 2008 17:20:16 +0100, Nicolas Pouillard wrote:
> >
> > Excerpts from christophe.troestler+ocaml's message of Mon Jan 28 17:06:10 +0100 2008:
> > > On Mon, 28 Jan 2008 15:25:12 +0000, Jon Harrop wrote:
> > > >
> > > > So you write a "use" binding:
> > > >
> > > >   let read_first_line file =
> > > >     use ch = open_in file in
> > > >     input_line ch
> > > >
> > > > and it gets translated into:
> > > >
> > > >   let read_first_line file =
> > > >     let ch = open_in file in
> > > >     try input_line ch finally
> > > >     ch#dispose
> > > >
> > > > where the "dispose" method that was automatically inserted at
> > > > the end of the scope of the "use" binding calls "close_in" in
> > > > this case to deallocate the external resource (a file handle in
> > > > this case but it could be anything with a dispose method).
> > >
> > > What is wrong with
> > >
> > >    let read_first_line file =
> > >      with_open_in file begin fun ch ->
> > >        input_line ch
> > >      end
> >
> > This  code  has  nothing  wrong but with_open_in (even if it's very useful) is
> > not resource safe. This is because the in_channel can escape it's scope.
> >
> > | let ch = with_open_in file (fun x -> x) in
> > | input_line ch
> >
> > This program is obviously wrong but still show that with_open_in is not safe.
>
> Right but how is it different with "use" ?

It's basically the same. But with "use" (or "using" in C#), you don't
have to write the "with_open_in" wrapper in the first place because
the finalizing function is implicit, it's the method called "Dispose".

Using this scheme with OCaml would mean that any resource-using
datatype has to be an object (not really the case right now).

-- 
  Olivier