From: Pierre Weis <Pierre.Weis@inria.fr>
To: walters@cis.ohio-state.edu (Colin Walters)
Cc: caml-list@inria.fr
Subject: Re: unwind-protect?
Date: Fri, 17 Nov 2000 13:28:46 +0100 (MET) [thread overview]
Message-ID: <200011171228.NAA21785@pauillac.inria.fr> (raw)
In-Reply-To: <87d7fvruas.church.of.emacs@meta.verbum.org> from Colin Walters at "Nov 17, 100 01:23:23 am"
> Hello,
>
> Is there an equivalent of Lisp's `unwind-protect' or Java's
> "try...finally"? I wanted to write a function which changed to
> another directory temporarily, and I came up with:
>
> let in_directory d f =
> let olddir = Unix.getcwd() in
> (Unix.chdir d;
> f();
> Unix.chdir olddir)
>
> But this won't work if f throws an exception. Any suggestions?
There is no special construct: the basic construct is powerful enough
to handle the construction: you just have to use pattern matching to
bound the exception and raise it again, after having properly set up
the global state.
let in_directory d f =
let olddir = Unix.getcwd () in
try
Unix.chdir d;
f ();
Unix.chdir olddir
with x -> Unix.chdir olddir; raise x;;
val in_directory : string -> (unit -> 'a) -> unit = <fun>
Note also that this scheme can be generalized to cases where you need
to apply a function to some argument and need to get the result of
that call:
let in_directory d f arg =
let olddir = Unix.getcwd () in
try
Unix.chdir d;
let r = f arg in
Unix.chdir olddir;
r
with x -> Unix.chdir olddir; raise x;;
val in_directory : string -> ('a -> 'b) -> 'a -> 'b = <fun>
Here, polymorphism, curryfication, and specialization are shining,
since you juste have to add one in_* functional application before an
actual call to any function f to obtain a specialized evaluation
context for f. For instance:
let in_tmp f arg = in_directory "/tmp" f arg;;
val in_tmp : ('a -> 'b) -> 'a -> 'b = <fun>
let open_out_tmp fname = in_tmp open_out fname;;
val open_out_tmp : string -> out_channel = <fun>
Hope this helps,
Pierre Weis
INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/
next prev parent reply other threads:[~2000-11-17 12:31 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-11-17 6:23 unwind-protect? Colin Walters
2000-11-17 12:28 ` Pierre Weis [this message]
2000-11-17 13:54 ` unwind-protect? CREGUT Pierre FTRD/DTL/LAN
2000-11-17 14:01 unwind-protect? Damien Doligez
2000-11-21 9:36 ` unwind-protect? Pierre Weis
2000-11-17 14:37 unwind-protect? David McClain
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=200011171228.NAA21785@pauillac.inria.fr \
--to=pierre.weis@inria.fr \
--cc=caml-list@inria.fr \
--cc=walters@cis.ohio-state.edu \
/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