caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* unwind-protect?
@ 2000-11-17  6:23 Colin Walters
  2000-11-17 12:28 ` unwind-protect? Pierre Weis
  0 siblings, 1 reply; 6+ messages in thread
From: Colin Walters @ 2000-11-17  6:23 UTC (permalink / raw)
  To: caml-list

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?




^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re: unwind-protect?
@ 2000-11-17 14:01 Damien Doligez
  2000-11-21  9:36 ` unwind-protect? Pierre Weis
  0 siblings, 1 reply; 6+ messages in thread
From: Damien Doligez @ 2000-11-17 14:01 UTC (permalink / raw)
  To: caml-list

>From: Pierre Weis <Pierre.Weis@inria.fr>

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

Actually, currying gets in the way in this case (as it often does).
If f takes two arguments, then "in_tmp f x y" will not work as
expected, but the type-checker will not find the error.

-- Damien



^ permalink raw reply	[flat|nested] 6+ messages in thread
* Re: unwind-protect?
@ 2000-11-17 14:37 David McClain
  0 siblings, 0 replies; 6+ messages in thread
From: David McClain @ 2000-11-17 14:37 UTC (permalink / raw)
  To: caml-list

>Is there an equivalent of Lisp's `unwind-protect' or Java's
>"try...finally"?

Here is the version that I use. It expects two functions of no arguments --
one for the protected portion and one for cleanup that is invoked in any
event.

let unwind_protect fn exitfn =
  let rslt =
    try
      fn()
    with
 exn ->
   ignore(exitfn());
   raise exn
  in
    ignore(exitfn());
    rslt


- D.McClain




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2000-11-21  9:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-17  6:23 unwind-protect? Colin Walters
2000-11-17 12:28 ` unwind-protect? Pierre Weis
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

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