caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: tim@fungible.com (Tim Freeman)
To: Christian.Schaller@siemens.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] closing file descriptors and channels
Date: Thu, 20 Nov 2003 09:54:09 -0700	[thread overview]
Message-ID: <20031120175959.81A7F4BED8@lobus.fungible.com> (raw)
In-Reply-To: <A1364BC6814D92479D4EED572D6F6FD806953C@uranus.certw2k.net> (Christian.Schaller@siemens.com)

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

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

let finally (body: unit -> 'a) (handler: unit -> unit): 'a =
  let result: 'a option ref = ref None
  in begin
      begin
        try begin
          result := Some (body ());
        end with e -> begin
          handler ();
          raise e;
        end;
      end;
      handler ();
      match !result with
          Some r -> r
        | _ -> 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 () ->
      let oc = "open the output file somehow" in
      finally (fun () ->
         .
	 .  (sometimes raise Found somewhere in here)
	 .
	 with 
	 End_of_file -> output_string oc "whatever")
      (fun () -> close_out oc))
   (fun () -> 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


  reply	other threads:[~2003-11-20 18:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-11-20 16:55 Christian Schaller
2003-11-20 16:54 ` Tim Freeman [this message]
2003-11-21 16:28   ` skaller
  -- strict thread matches above, loose matches on Subject: below --
2003-11-20 16:14 Christian Schaller
2003-11-20 16:35 ` Xavier Leroy
2003-11-20 17:01   ` Tim Freeman
2003-11-20 17:14   ` Nicolas George

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20031120175959.81A7F4BED8@lobus.fungible.com \
    --to=tim@fungible.com \
    --cc=Christian.Schaller@siemens.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).