From: "Yaron M. Minsky" <yminsky@cs.cornell.edu>
To: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] unwind-protect
Date: 30 Jul 2003 06:30:19 -0400 [thread overview]
Message-ID: <1059561019.25904.1468.camel@dragonfly.localdomain> (raw)
In-Reply-To: <3F275B5A.3010301@cs.caltech.edu>
I've more or less given up on this feature ever finding it's way into
the language, and I ended up writing my own, like many others. One of
the downsides of having people write their own unwind-protect's is that
it's a tad tricky to get right. The trick is ensuring that the cleanup
code only gets run once, even if an exception is thrown in the middle of
the cleanup. Here's my shot at it.
let protect ~f ~(finally: unit -> unit) =
let result = ref None in
try
result := Some (f ());
raise Exit
with
Exit as e ->
finally ();
(match !result with Some x -> x | None -> raise e)
| e ->
finally (); raise e
I definitely agree it would be nice to have this kind of thing in the
language, or at least in the standard library. When we give newbies
examples of how to read a file, giving them an example that uses
unwind-protect would prevent many future file-descriptor-exhaustion
bugs.
y
On Wed, 2003-07-30 at 01:44, Jason Hickey wrote:
> A related, but pragmatic problem. Suppose we have some imperative code,
> and we need an "unwind-protect" operation. Consider this code to make
> sure a file is closed after it is used.
>
> let with_open_file (name : string) (f : in_channel -> unit) =
> let inc = open_in name in
> try
> f inc;
> close inc
> with
> exn ->
> close inc;
> raise exn
>
> 1. It would be wonderful to have a "finally" clause, so we don't have to
> duplicate the close operation. I suppose we can fake it with CamlP4,
> but it seems better built-in.
>
> 2. This kind of exception re-raising does not work well with stack
> tracing (OCAMLRUNPARAM=b). It would be very nice to add a "finally"
> clause that did not interfere with stack tracing, or make it so that
> re-raising would preserve the location where the exception was created.
>
> Jason
--
|--------/ Yaron M. Minsky \--------|
|--------\ http://www.cs.cornell.edu/home/yminsky/ /--------|
Open PGP --- KeyID B1FFD916 (new key as of Dec 4th)
Fingerprint: 5BF6 83E1 0CE3 1043 95D8 F8D5 9F12 B3A9 B1FF D916
-------------------
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
next prev parent reply other threads:[~2003-07-30 10:30 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-07-28 18:34 [Caml-list] assert caught by try with _ Chris Hecker
2003-07-28 19:08 ` Nicolas Cannasse
2003-07-29 2:37 ` Chris Hecker
2003-07-29 3:17 ` Jacques Garrigue
2003-07-29 21:01 ` Chris Hecker
2003-07-30 10:22 ` Yaron M. Minsky
2003-07-30 15:47 ` james woodyatt
2003-08-06 12:19 ` Michal Moskal
2003-08-06 14:50 ` William Lovas
2003-08-06 17:44 ` Michal Moskal
2003-07-30 5:44 ` Jason Hickey
2003-07-30 5:44 ` [Caml-list] unwind-protect Jason Hickey
2003-07-30 10:30 ` Yaron M. Minsky [this message]
2003-07-30 17:29 ` Didier Remy
2003-07-31 0:47 ` Jacques Garrigue
2003-07-30 14:44 ` William Lovas
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=1059561019.25904.1468.camel@dragonfly.localdomain \
--to=yminsky@cs.cornell.edu \
--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