| 1 |
(* Copyright (C) 1999-2006, 2008 Henry Cejtin, Matthew Fluet, Suresh |
| 2 |
* Jagannathan, and Stephen Weeks. |
| 3 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 4 |
* |
| 5 |
* MLton is released under a HPND-style license. |
| 6 |
* See the file MLton-LICENSE for details. |
| 7 |
*) |
| 8 |
|
| 9 |
structure DynamicWind: DYNAMIC_WIND = |
| 10 |
struct |
| 11 |
|
| 12 |
fun try (f: unit -> 'a, k: 'a -> 'b, h: exn -> 'b) = |
| 13 |
let |
| 14 |
datatype t = |
| 15 |
A of 'a |
| 16 |
| E of exn |
| 17 |
in |
| 18 |
case A (f ()) handle e => E e of |
| 19 |
A a => k a |
| 20 |
| E e => h e |
| 21 |
end |
| 22 |
|
| 23 |
fun wind (thunk, cleanup: unit -> unit) = |
| 24 |
try (thunk, fn a => (cleanup (); a), fn e => (cleanup (); raise e)) |
| 25 |
|
| 26 |
end |