<?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="2003/11/e91ee45628b74c0778bbf6e062e9de53"
  from="tim@f..."
  author="tim@f..."
  date="2003-11-20T18:00:14"
  subject="Re: [Caml-list] closing file descriptors and channels"
  prev="2003/11/c8c057fcc1099e7256ffb1a4b070af84"
  next="2003/11/14cf1145f2220b2d5d5aa66a8883d5d9"
  prev-in-thread="2003/11/5e21cd78e94318bd8f496dda39b97d60"
  next-in-thread="2003/11/dea488310b571a08836a76b61cb663d6"
  prev-thread="2003/11/5bd426650020fd2fdf52e8c681dca310"
  next-thread="2003/11/ac68477a0da5d7dbb8ba9e0bbe74e550"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="RE: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/5e21cd78e94318bd8f496dda39b97d60"
  from="Christian Schaller &lt;Christian.Schaller@s...&gt;"
  author="Christian Schaller"
  date="2003-11-20T16:55:43"
  subject="RE: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/e91ee45628b74c0778bbf6e062e9de53"
  from="tim@f..."
  author="tim@f..."
  date="2003-11-20T18:00:14"
  subject="Re: [Caml-list] closing file descriptors and channels">
<msg 
  url="2003/11/dea488310b571a08836a76b61cb663d6"
  from="skaller &lt;skaller@o...&gt;"
  author="skaller"
  date="2003-11-21T17:29:23"
  subject="Re: [Caml-list] closing file descriptors and channels">
</msg>
</msg>
</msg>
</thread>

<contents>
From: "Christian Schaller" &lt;Christian.Schaller@siemens.com&gt;
&gt;Yup, I've seen this one already.  This one makes it complicated, since
&gt;for propagating the exception, I have to duplicate the closings:
&gt;
&gt;    .
&gt;    .
&gt;    .
&gt;  with          
&gt;  End_of_file -&gt; output_string oc (line ^ "\n");
&gt;  Found line -&gt; (close_out oc; close_in ic; raise Found line);
&gt;  close_out oc;
&gt;  close_in ic

Java's "try ... finally" is useful and can be recreated in ML.  Here's
a definition:

let finally (body: unit -&gt; 'a) (handler: unit -&gt; unit): 'a =
  let result: 'a option ref = ref None
  in begin
      begin
        try begin
          result := Some (body ());
        end with e -&gt; begin
          handler ();
          raise e;
        end;
      end;
      handler ();
      match !result with
          Some r -&gt; r
        | _ -&gt; failwith "Confused in finally";
    end;;

and here's how to use it for your example:

   let ic = "open the input file somehow" in
   finally (fun () -&gt;
      let oc = "open the output file somehow" in
      finally (fun () -&gt;
         .
	 .  (sometimes raise Found somewhere in here)
	 .
	 with 
	 End_of_file -&gt; output_string oc "whatever")
      (fun () -&gt; close_out oc))
   (fun () -&gt; close_in ic)

I have to admit Java's syntax for try...finally is better.
-- 
Tim Freeman                                                  tim@fungible.com
GPG public key fingerprint ECDF 46F8 3B80 BB9E 575D  7180 76DF FE00 34B1 5C78 
Your levity is good.  It relieves tension and fear of death.  -- Terminator 3

-------------------
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>

