<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/12/f0675ccbe1bf6bcbf69263f410121e17"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2002-12-12T13:17:08"
  subject="Re: [Caml-list] Resource acquisition is initialization"
  prev="2002/12/cb2f141f3a65bff4d89e1ee25b3f44fd"
  next="2002/12/d9879736886f7d0799a45d28d7845728"
  prev-in-thread="2002/12/bef5070e1bc3ca250bf2f818ad6ff8fa"
  next-in-thread="2002/12/d9879736886f7d0799a45d28d7845728"
  prev-thread="2002/12/a7df150482511ac2dc29aad949c1630c"
  next-thread="2002/12/e9bc0bae209d5d139109989b178cbc85"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/919e5b9de09938198c2fd0856d238c55"
  from="Blair Zajac &lt;blair@o...&gt;"
  author="Blair Zajac"
  date="2002-12-11T19:18:46"
  subject="[Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/5fc049891ea11b2acd98c914bdd66486"
  from="Brian Hurt &lt;brian.hurt@q...&gt;"
  author="Brian Hurt"
  date="2002-12-11T19:47:44"
  subject="Re: [Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/5170d849604617577c466f9ddbc70c6a"
  from="Chet Murthy &lt;chet@w...&gt;"
  author="Chet Murthy"
  date="2002-12-12T00:29:37"
  subject="Re: [Caml-list] Resource acquisition is initialization ">
</msg>
<msg 
  url="2002/12/2820617a735026a0db6d33ccaaf53c59"
  from="Alessandro Baretta &lt;alex@b...&gt;"
  author="Alessandro Baretta"
  date="2002-12-12T07:54:49"
  subject="Re: [Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/fa5147003546a7ff957dba5a5ab3012d"
  from="Brian Hurt &lt;brian.hurt@q...&gt;"
  author="Brian Hurt"
  date="2002-12-12T16:32:25"
  subject="Re: [Caml-list] Resource acquisition is initialization">
</msg>
</msg>
<msg 
  url="2002/12/4f5285419bbec1c865398ea6d1f21997"
  from="Mike Potanin &lt;potanin@m...&gt;"
  author="Mike Potanin"
  date="2002-12-13T09:08:43"
  subject="Re: [Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/bef5070e1bc3ca250bf2f818ad6ff8fa"
  from="David Brown &lt;caml-list@d...&gt;"
  author="David Brown"
  date="2002-12-13T17:05:51"
  subject="Re: [Caml-list] Resource acquisition is initialization">
</msg>
</msg>
</msg>
<msg 
  url="2002/12/f0675ccbe1bf6bcbf69263f410121e17"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2002-12-12T13:17:08"
  subject="Re: [Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/d9879736886f7d0799a45d28d7845728"
  from="Dmitry Bely &lt;dbely@m...&gt;"
  author="Dmitry Bely"
  date="2002-12-12T14:07:03"
  subject="Re: [Caml-list] Resource acquisition is initialization">
<msg 
  url="2002/12/5833c628ad7eb5017603c6a0f60ca7e8"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2002-12-12T14:16:07"
  subject="Re: [Caml-list] Resource acquisition is initialization">
</msg>
</msg>
<msg 
  url="2002/12/1fc22ef6af0290c23b787df542ce98bc"
  from="Blair Zajac &lt;blair@o...&gt;"
  author="Blair Zajac"
  date="2002-12-12T23:59:11"
  subject="Re: [Caml-list] Resource acquisition is initialization">
</msg>
<msg 
  url="2002/12/880189f28b91773cde73a547cf10094b"
  from="Quetzalcoatl Bradley &lt;qbradley@b...&gt;"
  author="Quetzalcoatl Bradley"
  date="2002-12-13T11:27:48"
  subject="Re: [Caml-list] Resource acquisition is initialization">
</msg>
</msg>
</msg>
</thread>

<contents>
&gt; One of the nice things about C++ and Java is that with properly
&gt; designed classes, you don't need to worry about freeing resources
&gt; in complicated code, because the when the objects go out of scope
&gt; 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).

&gt; Given that Ocaml has objects, it would be useful to have this
&gt; idiom available to us.  Is there a way to implement it, rather
&gt; 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 -&gt;
    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 -&gt;
    Mutex.unlock mut;
    raise x

You get the idea.

&gt; Also, since objects have initializers, do they have finializers?  I
&gt; read the entire Oreilly book and didn't see any mention of them.
&gt; Reading the C code interface, it looks like you can associate a
&gt; finalizer function to clean up an abstract type, but can you do
&gt; 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

</contents>

</message>

